C# 无法将方法组“”转换为非委托类型“System.Delegate”。您是否打算调用该方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15204639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot convert method group '' to non-delegate type 'System.Delegate'. Did you intend to invoke the method?
提问by Arsen Zahray
I'm trying to store a function reference in a Delegate type for later use.
我正在尝试将函数引用存储在 Delegate 类型中以供以后使用。
Here's what I'm doing:
这是我在做什么:
class Program
{
static void Test()
{
}
static void Main(string[] args)
{
Delegate t= (Delegate)Test;
}
}
In this I'm getting following error:
在这我收到以下错误:
Cannot convert method group 'Test' to non-delegate type 'System.Delegate'.
Did you intend to invoke the method?
无法将方法组“Test”转换为非委托类型“System.Delegate”。
您是否打算调用该方法?
Why is this happening?
为什么会这样?
采纳答案by Servy
You really shouldn't ever use the type Delegate
to store a delegate. You should be using a specific type of delegate.
您真的不应该使用该类型Delegate
来存储委托。您应该使用特定类型的委托。
In almost all cases you can use Action
or Func
as your delegate type. In this case, Action
is appropriate:
在几乎所有情况下,您都可以使用Action
或Func
作为您的委托类型。在这种情况下,Action
是合适的:
class Program
{
static void Test()
{
}
static void Main(string[] args)
{
Action action = Test;
action();
}
}
You can technically get an instance of Delegate
by doing this:
从技术上讲,您可以Delegate
通过执行以下操作获得一个实例:
Delegate d = (Action)Test;
But actually using a Delegate
, as opposed to an actual specific type of delegate, such as Action
, will be hard, since the compiler will no longer know what the signature of the method is, so it doesn't know what parameters should be passed to it.
但实际上使用 a Delegate
,而不是实际的特定类型的委托,例如Action
,会很困难,因为编译器将不再知道该方法的签名是什么,因此它不知道应该将哪些参数传递给它.
回答by Jon
What you are trying to do here is cast the method groupTest
to something. As per the spec, the only legal cast for a method group is casting it into a delegate type. This can be done either explicitly:
您在这里尝试做的是将方法组转换Test
为某种东西。根据规范,方法组的唯一合法强制转换是将其强制转换为委托类型。这可以明确地完成:
var t = (Delegate)Test;
or implicitly:
或隐含地:
Delegate t = Test;
However, as the documentationsays, System.Delegate
itself is... not a delegate type:
但是,正如文档所说,System.Delegate
它本身是......不是委托类型:
The Delegate class is the base class for delegate types. However, only the system and compilers can derive explicitly from the Delegate class or from the MulticastDelegate class. It is also not permissible to derive a new type from a delegate type. The
Delegate
class is not considered a delegate type; it is a class used to derive delegate types.
Delegate 类是委托类型的基类。但是,只有系统和编译器可以显式地从 Delegate 类或 MulticastDelegate 类派生。也不允许从委托类型派生新类型。所述
Delegate
类不被认为是委托类型; 它是一个用于派生委托类型的类。
The compiler detects this and complains.
编译器检测到这一点并抱怨。
If you want to cast a method group to a delegate you will have to specify a delegate type with a compatible signature (in this case, Action
).
如果要将方法组转换为委托,则必须指定具有兼容签名的委托类型(在本例中为Action
)。