12th Chapter commissioned [". NET Framework Program design" Reading notes]

Source: Internet
Author: User
Tags foreach instance method reflection
. NET Framework | notes | procedure | Design Chapter 12th commissioned

First, the use of the Commission

Static delegates and instance delegates, using the same method, gives an example of using a variable parameter delegate:

Using System;



public class Delcls

{

public delegate void Deldef (params string[] strparams);



public static void Calldel (Deldef dd)

{

if (dd!= null)//Please be sure to judge here, this is a good habit

{

DD ("Hello", "World");

}

}

}



public class Delins

{

declaring private (private) members does not affect the use of delegates within a type

private static void Clscallstr (params string[] strparams)/Type method

{

To output an array of strings and sequentially

foreach (String str in strparams)

{

Console.Write ("{0}", str);

}

Console.WriteLine ();

}



public void Inscallstr (params string[] strparams)//instance method

{

To output an array of strings and reverse order

for (int i = strparams.length-1 i >= 0; I-)

{

Console.Write ("{0}", Strparams[i]);

}



Console.WriteLine ();

}



public static void Main ()

{

Delins di = new Delins ();



DELCLS.DELDEF DD = null;

Console.WriteLine ("Combine two delegate:");

DD + + new Delcls.deldef (DELINS.CLSCALLSTR);

DD + + new Delcls.deldef (di. INSCALLSTR);

Delcls.calldel (DD);



Console.WriteLine ("Remove the delegate:");

DD-= new Delcls.deldef (DELINS.CLSCALLSTR);

Delcls.calldel (DD);

}

}



/* Run Results

Combine two delegate:

Hello World

World Hello

Remove the delegate:

World Hello

*/

To use the delegate method in C #:

L The method used to create the delegate must be consistent with the delegate declaration (parameter list, return values are consistent)

L Use + + = to link or unlink the delegate or to use the Delegate.combine and Delegate.remove methods directly to implement

L Use the MulticastDelegate instance method Getinvocationlist () to get all the delegates in the delegate chain



Ii. The disclosure of the Commission

All delegates inherit from MulticastDelegate, and the compiler generates a complete delegate class for the delegate's declaration at compile time, focusing on some of these members:

The constructor, the target object (instance) of the incoming delegate, and the integer that points to the callback method

U Inherit from MulticastDelegate _target (System.Object) field

U Inherit from MulticastDelegate _methodptr (System.Int32) field

U Inherit from MulticastDelegate _prev (system.multicastdelegaet) field

The generated and method declarations are consistent with the Invoke function used to invoke the method

The properties of _methodptr and _target fields can be investigated using the method and target properties in MulticastDelegate.

The invocation of compiler-generated delegate classes and invoke methods can be obtained by using ILDAsm.exe to view the IL code of the executing file

The main method in the type Delins in the previous example is modified to experiment with the use of properties in Getinvocationlist and MulticastDelegate:

public class Delins

{

...

public static void Main ()

{

...

delegate[] Arrdel = dd. Getinvocationlist ();

foreach (Delcls.deldef d in Arrdel)

{

Console.WriteLine ("Object type: {0}, method name: {1}",

(D.target!= null)? D.target.gettype (). ToString (): "Null",

D.method.name);

}

...

}

...

}

/* Run Results

...

Object Type:null, Method name:clscallstr

Object Type:delins, Method name:inscallstr

...

*/

Third, the Commission of the award

First, the _methodptr and _target fields are judged to be equal, and false is returned if unequal;

If it is equal, continue to determine whether _prev is null (a delegate to the head of the delegate chain), and if NULL, the equality returns true;

If not, then judge all the delegate objects on the delegate chain, and repeat the above steps.



It can be seen that it is a recursive judgment process when the chain of Entrustment is involved.

Four, Entrust chain

L The delegate that is first added to the delegate chain is at the end of the chain of the delegate, but is called first because Invoke uses recursion to invoke the delegate function so that the delegate at the head is finally invoked.

L The return value after the delegate invocation, only the return value of the last called method, that is, the return value of the delegate chain header

L Delete only the first delegate chain that matches every time the Remove method is invoked

V. Delegation and Reflection

The following is a list of the delegate.createdelegate methods provided by the. NET Framework SDK Documentation:

Creates a delegate of the specified type to represent the specified static method.

[C #] public static Delegate createdelegate (Type, MethodInfo);

Creates a delegate of the specified type that represents the specified instance method to invoke on the specified class instance.

[C #] public static Delegate createdelegate (Type, Object, String);

Creates a delegate of the specified type that represents the specified static method of the specified class.

[C #] public static Delegate createdelegate (type, type, string);

Creates a delegate of the specified type that represents the specified instance method to call on the specified class instance by the specified case sensitivity.

[C #] public static Delegate createdelegate (Type, Object, string, bool);



The following example shows the creation of static method delegates, instance method delegates, and dynamic invocation delegates:

Using System;

Using System.Reflection;



public class Delreflection

{

public delegate void GoGo (String strpam, Int32 npam);



public static void Clsgo (String strpam, Int32 npam)

{

Console.WriteLine ("In Class, string:{0}, Int32:{1}", Strpam, Npam);

}



public void Insgo (string strpam, Int32 npam)

{

Console.WriteLine ("In Instance, string:{0}, Int32:{1}", Strpam, Npam);

}



public static void Main ()

{

Delegate d = null;



D = Delegate.createdelegate (typeof (GoGo), typeof (Delreflection), "Clsgo");

if (d!= null)

D.dynamicinvoke (New object[]{"Hello", 45});



Delreflection dr = new Delreflection ();

D = Delegate.createdelegate (typeof (GoGo), Dr, "Insgo");

if (d!= null)

D.dynamicinvoke (New object[]{"Hello", 45});

}

}

/* Run Results

In class, String:hello, int32:45

In instance, String:hello, int32:45

*/


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.