C++)。但是,我收到异常:指定的类型不能是泛型类型定义。 据我所知,我没有传递通用类型定义-6ren">
gpt4 book ai didi

c# - 异常使用 GetFunctionPointerForDelegate : "The specified Type must not be a generic type definition. "

转载 作者:行者123 更新时间:2023-12-05 02:01:49 34 4
gpt4 key购买 nike

尝试使用 GetFunctionPointerForDelegate 通过互操作传递函数指针,(C# -> C++)。但是,我收到异常:指定的类型不能是泛型类型定义。

据我所知,我没有传递通用类型定义,在检查委托(delegate)类型时,我看到了以下内容: enter image description here

我写了一个最简单的例子并观察到同样的行为,如果有任何关于我遗漏的建议,我将不胜感激。

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

最佳答案

阅读错误并像这样尝试net fiddle :

        delegate int BarFunc(int a);
public void Foo()
{
BarFunc del = Bar;
IntPtr funcPtr = Marshal.GetFunctionPointerForDelegate(del);
}

public int Bar(int a)
{
return 0;
}

new Func<int, int>声明一个通用类型。为避免这种情况,请声明一个委托(delegate)类型并按照示例代码和 fiddle 中的指示进行分配。

关于c# - 异常使用 GetFunctionPointerForDelegate : "The specified Type must not be a generic type definition. ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66260123/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com