diff --git a/src/Generator/Generators/CSharp/CSharpMarshal.cs b/src/Generator/Generators/CSharp/CSharpMarshal.cs index 6e841fcad..21d7279ca 100644 --- a/src/Generator/Generators/CSharp/CSharpMarshal.cs +++ b/src/Generator/Generators/CSharp/CSharpMarshal.cs @@ -777,10 +777,18 @@ private void MarshalRefClass(Class @class) Context.Parameter.Name}"", ""Cannot be null because it is passed by value."");"); var nativeClass = typePrinter.PrintNative(@class); + var cctorName = CSharpSources.GetFunctionNativeIdentifier(Context.Context, cctor); + + var defaultValue = ""; + var TypePrinter = new CSharpTypePrinter(Context.Context); + var ExpressionPrinter = new CSharpExpressionPrinter(TypePrinter); + if (cctor.Parameters.Count > 1) + defaultValue = $", {ExpressionPrinter.VisitParameter(cctor.Parameters.Last())}"; + Context.Before.WriteLine($"byte* __{Context.Parameter.Name}Memory = stackalloc byte[sizeof({nativeClass})];"); Context.Before.WriteLine($"__IntPtr __{Context.Parameter.Name}Ptr = (__IntPtr)__{Context.Parameter.Name}Memory;"); - Context.Before.WriteLine($"{nativeClass}.{cctorName}(__{Context.Parameter.Name}Ptr, {Context.Parameter.Name}.__Instance);"); + Context.Before.WriteLine($"{nativeClass}.{cctorName}(__{Context.Parameter.Name}Ptr, {Context.Parameter.Name}.__Instance{defaultValue});"); Context.Return.Write($"__{Context.Parameter.Name}Ptr"); if (Context.Context.ParserOptions.IsItaniumLikeAbi && @class.HasNonTrivialDestructor)