Inside the delegates

  1. What does the C# compiler generate when we define delegate?
  2. Which methods are defined inside the System.MulticastDelegate?
  3. Why do we need the Delegate class?
  4. Which properties are defined inside the System.Delegate class?
  5. How to get information about a callback method which is contained inside a delegate?

When we define a delegate the C# compiler generate a new wrapper class which is derived from System.MulticastDelegate. The new class contains the constructor and three methods to call a callback method. The synchronize method Invoke has the signature as is defined by delegate. The BeginInvoke and EndInvoke methods serve to call callback methods asynchronously.

 

Example 1:

internal delegate Stream CallBack(int i);

it turns into

.class auto ansi sealed nested assembly CallBack extends [mscorlib]System.MulticastDelegate
{
    .method public hidebysig specialname rtspecialname instance void .ctor(object 'object', native int 'method') runtime managed
    {
    }
 
    .method public hidebysig newslot virtual instance class [mscorlib]System.IAsyncResult BeginInvoke(int32 i, class [mscorlib]System.AsyncCallback callback, object 'object') runtime managed
    {
    }
 
.method public hidebysig newslot virtual instance class [mscorlib]System.IO.Stream EndInvoke(class [mscorlib]System.IAsyncResult result) runtime managed
    {
    }
 
    .method public hidebysig newslot virtual instance class [mscorlib]System.IO.Stream Invoke(int32 i) runtime managed
    {
    }

}

So when we define

fb(i++);

it actually turns into

fb.Invoke(i++);

The System.MulticastDelegate class serves to call callback methods. It is derived from the Delegate class, which contains methods to manipulate its delegates and fields and properties to keep information about its delegates.

The Delegate class contains two important properties:

Method

public MethodInfo Method { get; }

Gets the metadata information about the method which is contained by delegate.

Target

public Object Target

{ get; }

Gets the reference to the class instance of the callback method. If it is static method the Target returns null.

 

            Feedback fb1 = new Feedback(StaticMethod);

            Feedback fb2 = new Feedback(p.InstanceMethod);

We can use these properties to get some information about callback methods

Example 2:

 

 

        private static void ShowMethodInfo(Feedback fb)

        {

            if (fb != null)

                Console.WriteLine(fb.Method.Name);

        }

 

        private static void ShowTargetInfo(Feedback fb)

        {

            if (fb != null)

            {

                if (fb.Target == null)

                    Console.WriteLine("A static method was called");

                else

                    Console.WriteLine("An instance method of class {0} was called", fb.Target.GetType().Name);

            }

        }

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s