diff --git a/src/Generator/Generators/CSharp/CSharpMarshal.cs b/src/Generator/Generators/CSharp/CSharpMarshal.cs index aa45d9684..8f64f72b2 100644 --- a/src/Generator/Generators/CSharp/CSharpMarshal.cs +++ b/src/Generator/Generators/CSharp/CSharpMarshal.cs @@ -776,10 +776,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)