diff --git a/src/Generator/Generators/CSharp/CSharpMarshal.cs b/src/Generator/Generators/CSharp/CSharpMarshal.cs index 943f075f..db418463 100644 --- a/src/Generator/Generators/CSharp/CSharpMarshal.cs +++ b/src/Generator/Generators/CSharp/CSharpMarshal.cs @@ -572,17 +572,24 @@ namespace CppSharp.Generators.CSharp return; } - if (Context.Parameter.Type.IsAddress()) + string param = Helpers.SafeIdentifier(Context.Parameter.Name); + Type type = Context.Parameter.Type.Desugar(); + if (type.IsAddress()) { - Context.Return.Write("{0}.{1}", Helpers.SafeIdentifier(Context.Parameter.Name), - Helpers.InstanceIdentifier); + Class decl; + if (type.IsTagDecl(out decl) && decl.IsValueType) + Context.Return.Write("{0}.{1}", param, Helpers.InstanceIdentifier); + else + Context.Return.Write("{0} == ({2}) null ? global::System.IntPtr.Zero : {0}.{1}", param, + Helpers.InstanceIdentifier, type); return; } var qualifiedIdentifier = CSharpMarshalNativeToManagedPrinter.QualifiedIdentifier( @class.OriginalClass ?? @class); - Context.Return.Write("*({0}.Internal*){1}.{2}", qualifiedIdentifier, - Helpers.SafeIdentifier(Context.Parameter.Name), Helpers.InstanceIdentifier); + Context.Return.Write( + "{1} == ({0}) null ? new {0}.Internal() : *({0}.Internal*) ({1}.{2})", qualifiedIdentifier, + param, Helpers.InstanceIdentifier); } private void MarshalValueClass(Class @class) diff --git a/tests/CSharpTemp/CSharpTemp.Tests.cs b/tests/CSharpTemp/CSharpTemp.Tests.cs index 37c9f1aa..1115bf84 100644 --- a/tests/CSharpTemp/CSharpTemp.Tests.cs +++ b/tests/CSharpTemp/CSharpTemp.Tests.cs @@ -33,7 +33,7 @@ public class CSharpTempTests [Test] public void TestFixedArrays() { - Qux qux = new Qux(); + Qux qux = new Qux(null); var array = new[] { 1, 2, 3 }; qux.Array = array; for (int i = 0; i < qux.Array.Length; i++) diff --git a/tests/CSharpTemp/CSharpTemp.cpp b/tests/CSharpTemp/CSharpTemp.cpp index cc8e5134..304fb1db 100644 --- a/tests/CSharpTemp/CSharpTemp.cpp +++ b/tests/CSharpTemp/CSharpTemp.cpp @@ -31,6 +31,16 @@ const Foo& Bar::operator[](int i) const return m_foo; } +Qux::Qux() +{ + +} + +Qux::Qux(Foo foo) +{ + +} + int Qux::farAwayFunc() const { return 20; diff --git a/tests/CSharpTemp/CSharpTemp.h b/tests/CSharpTemp/CSharpTemp.h index 4d6998d0..feebef6a 100644 --- a/tests/CSharpTemp/CSharpTemp.h +++ b/tests/CSharpTemp/CSharpTemp.h @@ -21,6 +21,8 @@ protected: class DLL_API Qux { public: + Qux(); + Qux(Foo foo); int farAwayFunc() const; int array[3]; };