diff --git a/src/Generator/Generators/CLI/CLIMarshal.cs b/src/Generator/Generators/CLI/CLIMarshal.cs index 1ad00043..551b4095 100644 --- a/src/Generator/Generators/CLI/CLIMarshal.cs +++ b/src/Generator/Generators/CLI/CLIMarshal.cs @@ -185,8 +185,9 @@ namespace CppSharp.Generators.CLI instance += "&"; instance += Context.ReturnVarName; + var needsCopy = !(Context.Declaration is Field); - if (@class.IsRefType) + if (@class.IsRefType && needsCopy) { var name = Generator.GeneratedIdentifier(Context.ReturnVarName); Context.SupportBefore.WriteLine("auto {0} = new ::{1}({2});", name, diff --git a/src/Generator/Generators/CLI/CLISourcesTemplate.cs b/src/Generator/Generators/CLI/CLISourcesTemplate.cs index b13e854c..6625d2a6 100644 --- a/src/Generator/Generators/CLI/CLISourcesTemplate.cs +++ b/src/Generator/Generators/CLI/CLISourcesTemplate.cs @@ -336,6 +336,7 @@ namespace CppSharp.Generators.CLI var ctx = new MarshalContext(Driver) { + Declaration = decl, ArgName = decl.Name, ReturnVarName = variable, ReturnType = decl.QualifiedType diff --git a/src/Generator/Generators/Marshal.cs b/src/Generator/Generators/Marshal.cs index 783179f5..8129b2be 100644 --- a/src/Generator/Generators/Marshal.cs +++ b/src/Generator/Generators/Marshal.cs @@ -19,6 +19,8 @@ namespace CppSharp.Generators public TextGenerator SupportBefore { get; private set; } public TextGenerator Return { get; private set; } + public Declaration Declaration { get; set; } + public string ReturnVarName { get; set; } public QualifiedType ReturnType { get; set; }