diff --git a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs index 13749b14..01ffdc99 100644 --- a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs +++ b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs @@ -2651,17 +2651,15 @@ namespace CppSharp.Generators.CSharp { PushBlock(CSharpBlockKind.Typedef); var attributedType = typedef.Type.GetPointee() as AttributedType; - if (attributedType != null) - { - var equivalentFunctionType = attributedType.Equivalent.Type as FunctionType; - var callingConvention = equivalentFunctionType.CallingConvention.ToInteropCallConv(); - if (callingConvention != System.Runtime.InteropServices.CallingConvention.Winapi) - { - WriteLine("[UnmanagedFunctionPointerAttribute(global::System.Runtime.InteropServices.CallingConvention.{0})]", - callingConvention); - } - } + var callingConvention = attributedType == null + ? functionType.CallingConvention + : ((FunctionType) attributedType.Equivalent.Type).CallingConvention; TypePrinter.PushContext(CSharpTypePrinterContextKind.Native); + var interopCallConv = callingConvention.ToInteropCallConv(); + if (interopCallConv != System.Runtime.InteropServices.CallingConvention.Winapi) + WriteLine( + "[UnmanagedFunctionPointerAttribute(global::System.Runtime.InteropServices.CallingConvention.{0})]", + interopCallConv); WriteLine("{0}unsafe {1};", Helpers.GetAccess(typedef.Access), string.Format(TypePrinter.VisitDelegate(functionType).Type, diff --git a/tests/Basic/Basic.Tests.cs b/tests/Basic/Basic.Tests.cs index cfde0225..e39855c7 100644 --- a/tests/Basic/Basic.Tests.cs +++ b/tests/Basic/Basic.Tests.cs @@ -418,5 +418,11 @@ public class BasicTests : GeneratorTestFixture Assert.AreEqual(bar.A << 2, result.valueTypeField.A); Assert.AreEqual(bar.B, result.valueTypeField.B); } + + [Test] + public void TestMarshalUnattributedDelegate() + { + new TestDelegates().MarshalUnattributedDelegate(i => i); + } } \ No newline at end of file diff --git a/tests/Basic/Basic.cpp b/tests/Basic/Basic.cpp index 1dd4b829..5ed172db 100644 --- a/tests/Basic/Basic.cpp +++ b/tests/Basic/Basic.cpp @@ -304,3 +304,7 @@ Bar::Item operator |(Bar::Item left, Bar::Item right) void va_listFunction(va_list v) { } + +void TestDelegates::MarshalUnattributedDelegate(DelegateInGlobalNamespace del) +{ +} diff --git a/tests/Basic/Basic.h b/tests/Basic/Basic.h index 3b5f1e0f..a3c0bf42 100644 --- a/tests/Basic/Basic.h +++ b/tests/Basic/Basic.h @@ -292,6 +292,7 @@ struct DLL_API TestDelegates int StdCall(DelegateStdCall del) { return del(1); } int CDecl(DelegateCDecl del) { return del(1); } + void MarshalUnattributedDelegate(DelegateInGlobalNamespace del); DelegateInClass A; DelegateInGlobalNamespace B;