2010-08-26 48 views
2

我很好奇代表方法的存在?比如我知道的異步方法調用,就像這樣:dot.Net代表有多少種方法?

class Program { 
    // define a delegate 
    delegate int MyDelegate(String s); 

    static void Main(string[] args) { 
     // create the delegate 
     MyDelegate del = new MyDelegate(myMethod); 

     // invoke the method asynchronously 
     IAsyncResult result = del.BeginInvoke("foo", null, null); 

     // get the result of that asynchronous operation 
     int retValue = del.EndInvoke(result); 

     } 
    } 

這裏是「的BeginInvoke()」和「EndInvoke會()」方法,但是否有任何其他代表的方法呢?

回答

6

所有代表類型派生自System.Delegate(就像所有的枚舉類型都來自System.Enum),這意味着它們都具有this page上的所有方法。

的值得注意的是:

DynamicInvoke
GetInvocationList

Delegate類型,這是非常有趣的,完全值得了解(因爲它可以把性能不佳的反射碼成比比編譯代碼的static方法)是CreateDelegate

另外:EqualsGetHashCode(是的,它們被覆蓋)。

直到最近我還是沒有意識到MethodTarget屬性,但我可以想象它們在某些特定環境下會非常有用。

+0

非常感謝。我有另一個問題。我遵循你的鏈接,並在msdn庫中的MulticastDelegate Class引用中提到「公共語言運行時提供了兩種特殊方法:BeginInvoke和EndInvoke」。只是好奇心,你知道在哪裏宣佈這些方法嗎? – kofucii 2010-08-26 20:03:46

相關問題