简体   繁体   中英

Delegate inherited methods explanation

Where methods BeginInvoke, Invoke, EndInvoke goes from?

I went to MulticastDelegate and Delegate, and they doesn't contain any methods declarations. Of course I understand that signature of this method depends on delegate declarations. But I can't understand how it works.

Here what John Skeet says about it:

Any delegate type you create has the members inherited from its parent types, one constructor with parameters of object and IntPtr and three extra methods: Invoke, BeginInvoke and EndInvoke. We'll come back to the constructor in a minute. The methods can't be inherited from anything, because the signatures vary according to the signature the delegate is declared with. Using the sample code above, the first delegate has the following methods

I'm not native English speaker and I'm a bit confused with fact that

Any delegate type you create has the members inherited from its parent

but then

The methods can't be inherited from anything

Please explain how it works.

Delegates are special types, that sentence probably means that you can't manually inherit from Delegate or MulticastDelegate class because they are special classes.So the C# compiler creates types that inherits from MulticastDelegate and declares those methods according to the signature of delegate type automatically.

Or possibly, it means that since the delegate type changes the signatures of those methods, they are not inherited but instead they created by compiler from scratch , depending on the type of the delegate.(after re-reading, this makes more sense).

When the compiler processes the C# delegate type it automatically generates a sealed class deriving from System.MulticastDelegate.

sealed class Add : System.MulticastDelegate
{
   public int Invoke(int x, int y);
   public IAsyncResult BeginInvoke(int x, int y, AsyncCallback cb, object state);
   public int EndInvoke(IAsyncResult result);
}

QuiteCode

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM