![](/img/trans.png)
[英]How do I fix 'compiler error - cannot convert from method group to System.Delegate'?
[英]how do i assign a static method to a System.Delegate object?
我的問題是,我有一個其構造函數將System.Delegate對象作為參數的類,並且我不知道如何為System.Delegate對象分配方法。 這是我現在擁有的代碼
class TestClass
{
Delegate c = TestMetod;
static void TestMetod()
{
MessageBox.Show("it worked !");
}
}
但這是行不通的,因為奇怪的是,如msdna所述,System.Delegate是非代理類型。 我應該怎么做我需要做的事情,因為不可能“將方法組TestMetod分配給非委托類型'System.Delegate'”
static
方面不是這里的核心問題。 您需要一個(任何)委托來捕獲TestMethod,然后可以將其分配給System.Delegate。 您可以將Action
用作中介。
class TestClass
{
static Action a = TestMetod;
static Delegate c = a;
static void TestMetod()
{
MessageBox.Show("it worked !");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.