首页 > 解决方案 > 动态添加 UnmanagedFunctionPointerAttribute 到委托

问题描述

我有一个 CreateDelegate 方法,我使用

Marshal.GetDelegateForFunctionPointer(func_addr, CreateDelegate())

它工作得很好,但现在我需要将其作为 ThisCall 进行编组。所以 func_addr 是一个“CallingConvention.ThisCall”。静态地,这是有效的:

[UnmanagedFunctionPointer(CallingConvention.ThisCall)]
delegate int d_Add(IntPtr _this, int x, int y);
...
var value = Marshal.GetDelegateForFunctionPointer<d_Add>(Addfunc_addr)(_this, 50, 100);
// return 150;

我想在运行时将 UnmanagedFunctionPointerAttribute 添加到来自 CreateDelegate 的新委托,或者以任何方式将我的委托编组为 ThisCall,这可能吗?

我找到了这个线程:在运行时添加自定义属性,但在我的情况下它不是自定义属性。

标签: c#attributesruntimemarshalling

解决方案


推荐阅读