diff --git a/src/Generator/Generators/CLI/CLIMarshal.cs b/src/Generator/Generators/CLI/CLIMarshal.cs index 3593527b..c241c3db 100644 --- a/src/Generator/Generators/CLI/CLIMarshal.cs +++ b/src/Generator/Generators/CLI/CLIMarshal.cs @@ -52,6 +52,8 @@ namespace CppSharp.Generators.CLI if (array.Type.IsPointerToPrimitiveType(PrimitiveType.Void)) Context.Before.WriteLineIndent("{0}[i] = new ::System::IntPtr({1}[i]);", value, Context.ReturnVarName); + else if (!array.Type.IsPrimitiveType()) + Context.Before.WriteLineIndent($"{value}[i] = gcnew {array.Type.ToString().TrimEnd('^')}(&{Context.ReturnVarName}[i]);"); else Context.Before.WriteLineIndent("{0}[i] = {1}[i];", value, Context.ReturnVarName); Context.Before.UnindentAndWriteCloseBrace(); @@ -436,13 +438,28 @@ namespace CppSharp.Generators.CLI } else { + bool isPointerToPrimitive = array.Type.IsPointerToPrimitiveType(PrimitiveType.Void); + bool isPrimitive = array.Type.IsPrimitiveType(); var supportBefore = Context.Before; supportBefore.WriteLine("if ({0} != nullptr)", Context.ArgName); supportBefore.WriteOpenBraceAndIndent(); + + string nativeVal = string.Empty; + if (isPointerToPrimitive) + { + nativeVal = ".ToPointer()"; + } + else if (!isPrimitive) + { + nativeVal = "->NativePtr"; + } + supportBefore.WriteLine("for (int i = 0; i < {0}; i++)", array.Size); - supportBefore.WriteLineIndent("{0}[i] = {1}[i]{2};", - Context.ReturnVarName, Context.ArgName, - array.Type.IsPointerToPrimitiveType(PrimitiveType.Void) ? ".ToPointer()" : string.Empty); + supportBefore.WriteLineIndent("{0}[i] = {1}{2}[i]{3};", + Context.ReturnVarName, + isPointerToPrimitive || isPrimitive ? string.Empty : "*", + Context.ArgName, + nativeVal); supportBefore.UnindentAndWriteCloseBrace(); } break; diff --git a/tests/Common/Common.Tests.cs b/tests/Common/Common.Tests.cs index 984fd6af..59a12641 100644 --- a/tests/Common/Common.Tests.cs +++ b/tests/Common/Common.Tests.cs @@ -981,4 +981,24 @@ This is a very long string. This is a very long string. This is a very long stri Assert.That(ret, Is.EqualTo(10)); } } + + [Test] + public void TestNonPrimitiveFixedArrays() + { + using (var nonPrimitiveFixedArray = new TestFixedNonPrimitiveArrays()) + { + nonPrimitiveFixedArray.NonPrimitiveTypeArray = new NonPrimitiveType[] + { + new NonPrimitiveType{ foo = 1 }, + new NonPrimitiveType{ foo = 2 }, + new NonPrimitiveType{ foo = 3 } + }; + + Assert.AreEqual(3, nonPrimitiveFixedArray.NonPrimitiveTypeArray.Length); + + Assert.AreEqual(1, nonPrimitiveFixedArray.NonPrimitiveTypeArray[0].Foo); + Assert.AreEqual(2, nonPrimitiveFixedArray.NonPrimitiveTypeArray[1].Foo); + Assert.AreEqual(3, nonPrimitiveFixedArray.NonPrimitiveTypeArray[2].Foo); + } + } } diff --git a/tests/Common/Common.cpp b/tests/Common/Common.cpp index a5924342..2cbdddc3 100644 --- a/tests/Common/Common.cpp +++ b/tests/Common/Common.cpp @@ -1181,3 +1181,12 @@ BaseCovariant::~BaseCovariant() DerivedCovariant::~DerivedCovariant() { } + +int NonPrimitiveType::GetFoo() +{ + return foo; +} + +TestFixedNonPrimitiveArrays::TestFixedNonPrimitiveArrays() +{ +} \ No newline at end of file diff --git a/tests/Common/Common.h b/tests/Common/Common.h index 437cab07..01d6b994 100644 --- a/tests/Common/Common.h +++ b/tests/Common/Common.h @@ -773,6 +773,22 @@ TestArraysPointers::TestArraysPointers(MyEnum *values, int count) if (values && count) Value = values[0]; } +class DLL_API NonPrimitiveType +{ + +public: + int GetFoo(); + + int foo; +}; + +class DLL_API TestFixedNonPrimitiveArrays +{ +public: + TestFixedNonPrimitiveArrays(); + NonPrimitiveType NonPrimitiveTypeArray[3]; +}; + struct DLL_API TestGetterSetterToProperties { int getWidth();