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:
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)
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.