diff --git a/src/Generator/Generators/CSharp/CSharpMarshal.cs b/src/Generator/Generators/CSharp/CSharpMarshal.cs index 7df4ca1c..4d17cabe 100644 --- a/src/Generator/Generators/CSharp/CSharpMarshal.cs +++ b/src/Generator/Generators/CSharp/CSharpMarshal.cs @@ -862,12 +862,15 @@ namespace CppSharp.Generators.CSharp var intermediateArray = $"__{Context.Parameter.Name}"; var intermediateArrayType = typePrinter.PrintNative(arrayType); - const string intPtrType = CSharpTypePrinter.IntPtrType; + var intPtrZero = $"{CSharpTypePrinter.IntPtrType}.Zero"; Context.Before.WriteLine($"{intermediateArrayType}[] {intermediateArray};"); Context.Before.WriteLine($"if (ReferenceEquals({Context.Parameter.Name}, null))"); - Context.Before.WriteLineIndent($"{intermediateArray} = new[] {{ {intPtrType}.Zero }};"); + if (arrayType.IsAddress()) + Context.Before.WriteLineIndent($"{intermediateArray} = new[] {{ {intPtrZero} }};"); + else + Context.Before.WriteLineIndent($"{intermediateArray} = new[] {{ new {intermediateArrayType}() }};"); Context.Before.WriteLine("else"); Context.Before.WriteStartBraceIndent(); @@ -878,8 +881,13 @@ namespace CppSharp.Generators.CSharp Context.Before.WriteStartBraceIndent(); const string element = "__element"; Context.Before.WriteLine($"var {element} = {Context.Parameter.Name}[i];"); - Context.Before.WriteLine($@"{intermediateArray}[i] = ReferenceEquals({ - element}, null) ? {intPtrType}.Zero : {element}.{Helpers.InstanceIdentifier};"); + if (arrayType.IsAddress()) + Context.Before.WriteLine($@"{intermediateArray}[i] = ReferenceEquals({ + element}, null) ? {intPtrZero} : {element}.{Helpers.InstanceIdentifier};"); + else + Context.Before.WriteLine($@"{intermediateArray}[i] = ReferenceEquals({ + element}, null) ? new {intermediateArrayType}() : *({ + intermediateArrayType}*) {element}.{Helpers.InstanceIdentifier};"); Context.Before.WriteCloseBraceIndent(); Context.Before.WriteCloseBraceIndent(); diff --git a/tests/CSharp/CSharp.Tests.cs b/tests/CSharp/CSharp.Tests.cs index 3fcae60d..189d6361 100644 --- a/tests/CSharp/CSharp.Tests.cs +++ b/tests/CSharp/CSharp.Tests.cs @@ -1063,9 +1063,10 @@ public unsafe class CSharpTests : GeneratorTestFixture [Test] public void TestArrayParams() { - Foo[] foos = { new Foo { A = 2 }, new Foo { A = 5 } }; + Foo[] pointers = { new Foo { A = 2 }, new Foo { A = 5 } }; int[] ints = { 6, 7 }; - Assert.That(CSharp.CSharp.TakeArrays(foos, ints), Is.EqualTo(20)); + Foo[] values = { new Foo { A = 10 }, new Foo { A = 20 } }; + Assert.That(CSharp.CSharp.TakeArrays(pointers, ints, values), Is.EqualTo(50)); } private class OverrideVirtualTemplate : VirtualTemplate diff --git a/tests/CSharp/CSharp.cpp b/tests/CSharp/CSharp.cpp index d973b55f..3961ec6c 100644 --- a/tests/CSharp/CSharp.cpp +++ b/tests/CSharp/CSharp.cpp @@ -1392,7 +1392,9 @@ void InlineNamespace::FunctionInsideInlineNamespace() { } -int takeArrays(Foo* arrayOfPointersToObjects[], int arrayOfPrimitives[]) +int takeArrays(Foo* arrayOfPointersToObjects[], int arrayOfPrimitives[], Foo arrayOfObjects[]) { - return arrayOfPointersToObjects[0]->A + arrayOfPointersToObjects[1]->A + arrayOfPrimitives[0] + arrayOfPrimitives[1]; + return arrayOfPointersToObjects[0]->A + arrayOfPointersToObjects[1]->A + + arrayOfPrimitives[0] + arrayOfPrimitives[1] + + arrayOfObjects[0].A + arrayOfObjects[1].A; } diff --git a/tests/CSharp/CSharp.h b/tests/CSharp/CSharp.h index b36498d3..02c1f1e6 100644 --- a/tests/CSharp/CSharp.h +++ b/tests/CSharp/CSharp.h @@ -1237,4 +1237,4 @@ inline namespace InlineNamespace DLL_API void FunctionInsideInlineNamespace(); } -DLL_API int takeArrays(Foo* arrayOfPointersToObjects[], int arrayOfPrimitives[]); +DLL_API int takeArrays(Foo* arrayOfPointersToObjects[], int arrayOfPrimitives[], Foo arrayOfObjects[]);