2011-02-25 77 views

回答

6

是,之前的.NET 2您必须手动指定:

ABC.eventX+=new X(someMethod); 

但它现在有了这个语法隐式创建:

ABC.eventX+=someMethod; 
+0

你不妨提一提,这就是所谓的*方法组转换*,万一OP想要了解更多信息。 – 2011-02-25 07:57:20

0

是的,它会自动创建。

例如:


namespace ConsoleApplication5 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      (new Program()).Entrance(); 
     } 

     public void Entrance() 
     { 
      ABC a = new ABC(); 
      a.eventX += callback; 
     } 

     protected bool callback(int a) 
     { 
      return true; 
     } 
    } 

    class ABC 
    { 
     public delegate bool X(int a); 
     public event X eventX; 
    } 
} 

程序类将是这个,如果你在反射镜看到:


internal class Program 
{ 
    // Methods 
    protected bool callback(int a) 
    { 
     return true; 
    } 

    public void Entrance() 
    { 
     ABC a = new ABC(); 
     a.eventX += new ABC.X(this.callback); 
    } 

    private static void Main(string[] args) 
    { 
     new Program().Entrance(); 
    } 
}