【发布时间】:2021-02-18 20:41:07
【问题描述】:
尝试使用GetFunctionPointerForDelegate 通过互操作传递函数指针(C# -> C++)。但是,我得到了例外:The specified Type must not be a generic type definition.
据我所知,我没有传入泛型类型定义,在检查委托的类型时,我看到以下内容:
我写了一个最小的例子并观察到相同的行为,如果我想知道什么,我将不胜感激。
using System;
using System.Runtime.InteropServices;
public class MyTestClass
{
public void Foo()
{
Delegate del = new Func<int, int>(Bar);
IntPtr funcPtr = Marshal.GetFunctionPointerForDelegate(del);
}
public int Bar(int a)
{
return 0;
}
}
public class Program
{
public static void Main()
{
var mtc = new MyTestClass();
mtc.Foo();
}
}
可以在 dotnet fiddle 上观察到该问题:https://dotnetfiddle.net/8Aol9j
【问题讨论】: