diff --git a/src/Generator/Generators/CSharp/CSharpSources.cs b/src/Generator/Generators/CSharp/CSharpSources.cs index 1ac93648..620be3de 100644 --- a/src/Generator/Generators/CSharp/CSharpSources.cs +++ b/src/Generator/Generators/CSharp/CSharpSources.cs @@ -1448,14 +1448,18 @@ namespace CppSharp.Generators.CSharp WriteStartBraceIndent(); WriteLine("_Thunks = new void*[{0}];", wrappedEntries.Count); + var uniqueEntries = new HashSet(); + for (int i = 0; i < wrappedEntries.Count; i++) { var entry = wrappedEntries[i]; var method = entry.Method; var name = GetVTableMethodDelegateName(method); - WriteLine($@"_Thunks[{i}] = Marshal.GetFunctionPointerForDelegate(new { - GetDelegateName(method, @class.TranslationUnit.Module.OutputNamespace) - }({name}Hook)).ToPointer();"); + var instance = name + "Instance"; + if (uniqueEntries.Add(entry)) + WriteLine("{0} += {1}Hook;", instance, name); + WriteLine("_Thunks[{0}] = Marshal.GetFunctionPointerForDelegate({1}).ToPointer();", + i, instance); } WriteCloseBraceIndent(); @@ -1723,6 +1727,11 @@ namespace CppSharp.Generators.CSharp var vTableMethodDelegateName = GetVTableMethodDelegateName(method); + WriteLine("private static {0} {1}Instance;", + GetDelegateName(method, @class.TranslationUnit.Module.OutputNamespace), + vTableMethodDelegateName); + NewLine(); + WriteLine("private static {0} {1}Hook({2})", retType, vTableMethodDelegateName, string.Join(", ", @params)); WriteStartBraceIndent();