diff --git a/src/Generator/Generators/CSharp/CSharpMarshal.cs b/src/Generator/Generators/CSharp/CSharpMarshal.cs index 4d079c4a..a778d7f3 100644 --- a/src/Generator/Generators/CSharp/CSharpMarshal.cs +++ b/src/Generator/Generators/CSharp/CSharpMarshal.cs @@ -286,6 +286,7 @@ namespace CppSharp.Generators.CSharp { if (Context.MarshalKind == MarshalKind.NativeField || Context.MarshalKind == MarshalKind.Variable || + Context.MarshalKind == MarshalKind.ReturnVariableArray || !originalClass.HasNonTrivialDestructor) { Context.Return.Write($"{qualifiedClass}.{Helpers.CreateInstanceIdentifier}({Context.ReturnVarName})"); diff --git a/src/Generator/Generators/CSharp/CSharpSources.cs b/src/Generator/Generators/CSharp/CSharpSources.cs index 6c21df1f..390ce327 100644 --- a/src/Generator/Generators/CSharp/CSharpSources.cs +++ b/src/Generator/Generators/CSharp/CSharpSources.cs @@ -1294,9 +1294,12 @@ namespace CppSharp.Generators.CSharp ctx.PushMarshalKind(MarshalKind.ReturnVariableArray); var arrayType = var.Type.Desugar() as ArrayType; - var isRefTypeArray = arrayType != null && var.Namespace is Class @class && @class.IsRefType; + var isRefTypeArray = arrayType != null && var.Namespace is Class context && context.IsRefType; var elementType = arrayType?.Type.Desugar(); - if (!isRefTypeArray && elementType == null) + Type type = (var.QualifiedType.Type.GetFinalPointee() ?? var.QualifiedType.Type).Desugar(); + if (type.TryGetClass(out Class @class) && @class.IsRefType) + ctx.ReturnVarName = $"new {TypePrinter.IntPtrType}({ptr})"; + else if (!isRefTypeArray && elementType == null) ctx.ReturnVarName = $"*{ptr}"; else if (elementType == null || elementType.IsPrimitiveType() || arrayType.SizeType == ArrayType.ArraySize.Constant) diff --git a/tests/Common/Common.Tests.cs b/tests/Common/Common.Tests.cs index d26ab18f..9eee5630 100644 --- a/tests/Common/Common.Tests.cs +++ b/tests/Common/Common.Tests.cs @@ -914,6 +914,8 @@ public class CommonTests Assert.That(Foo.ReadWrite, Is.EqualTo(15)); Foo.ReadWrite = 25; Assert.That(Foo.ReadWrite, Is.EqualTo(25)); + Foo.StaticField.A = 20; + Assert.That(Foo.StaticField.A, Is.EqualTo(20)); } [Test] diff --git a/tests/Common/Common.cpp b/tests/Common/Common.cpp index d3c96515..48a72887 100644 --- a/tests/Common/Common.cpp +++ b/tests/Common/Common.cpp @@ -60,6 +60,8 @@ char16_t Foo::returnChar16() return 'a'; } +Foo Foo::staticField; + Foo2::Foo2() {} Foo2::Foo2(const Foo2& other) : Foo(other), C(other.C), valueTypeField(other.valueTypeField) {} diff --git a/tests/Common/Common.h b/tests/Common/Common.h index d60a5bb0..3f9c9c0c 100644 --- a/tests/Common/Common.h +++ b/tests/Common/Common.h @@ -105,6 +105,8 @@ public: int fooPtr(); char16_t returnChar16(); + + static Foo staticField; }; struct DLL_API Bar