Chapter 17 Entrusted
First, the use of entrusted
Static commission and instance delegate, similar, here, give an example of using variable parameters:
Using system;
Public Class DelCls
{
Public Delegate void Deldef (params string ";
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
{
// Statement to Private (Private) member does not affect the use of internal use within the type
Private static void clscallstr (params string [] strparams // type method
{
/ / Output the string array and sequentially
Foreach (String Str in strparams)
{
Console.write ("{0}", STR);
}
Console.writeLine ();
}
Public void inssallstr (params string [] strparams) // instance method
{
/ / Output the string array and output
For (int i = strparams.lendth - 1; i> = 0; i -)
{
Console.write ("{0}", strparams [i]);
}
Console.writeLine ();
}
Public static void
Main
()
{
Delins di = new delins ();
Delcls.deldef DD = NULL;
"" Combine Two delegate: ");
DD = New Delcls.deldef (Delins.clscallstr);
DD = new delcls.deldef (di.inscallstr);
Delcls.callDel (DD);
Console.writeline ("Remove the first delegate:");
DD - = new delcls.deldef (delins.clscallstr);
Delcls.callDel (DD);
}
}
/*operation result
Combine Two delegate:
Hello World
World Hello
REMOVE The First Delegate:
World Hello
* /
Use the entrust method in C #:
l Creating the method used by the commission must be consistent with the delegate statement (the parameter list, the return value is consistent)
l Use =, - = to make a link or cancel link or use the delegate.comBBine and Delegate.Remove method.
l Use multicastDelegate's instance method getInvocationList () to get all the commissions in the commission chain.
Second, the commission
All entrustments have inherited from MulticastDelegate, the compiler generated a complete commission class in the compile time for the delegated statement, focusing on some of the members:
ü Constructor, incoming target object (instance) and integersion to the callback method
ü Inherits from multicastDelegate's _target (System.Object) field ü inherits the _methodptr (System.Int32) field from multicastdelegate
ü Inherits the _prev (System.MulticastDelegaet "field from MulticastDelegate
ü Generated and method declaration of the consistent input of the INVOKE function to call the method
You can use the MulticastDelegate in Method and Target properties to examine the nature of _methodptr and _target fields.
Regarding the call of the commissioner and the invoke method of the invoke method, you can get the IL code of the execution file using ILDASM.EXE.
Make the main method in the above-described type delins as follows to use the properties of the 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;
}
...
}
...
}
/*operation result
...
Object Type: Null, Method Name: CLSCallstr
Object Type: Delins, Method Name: Inscallstr
...
* /
Third, the commission
First determine whether the _methodptr and _target fields are equal, if it is not equal, it returns false;
If you are equal, continue to determine whether _prev is NULL (point to the delegate chain head), if null, equally returns true;
If it is not equal, it will be judged to determine all the delegate objects on the commission chain and repeat the above steps.
It can be seen that it is a process of recursive judgment when it involves the commission chain.
Fourth, the commission chain
l Firstly, the delegation is located in the end of the delegate chain, but first calls because the recursive delegate function is called in Invoke, which is last called.
l The return value after the commissioned call is only the return value of the last time called method, that is, the return value of the commissioned chain head.
l Each time the REMOVE method is called, only the first delegation chain matching.
V. Entrusted and reflection
The following is a list of Delegate.createdElegate methods provided by the .NET Framework SDK document:
Create a delegate of the specified type to represent the specified static method.
[C #] public static delegate CreateDelegate (Type, MethodInfo);
Create a delegate of the specified type that indicates the specified instance method to be called for the specified class instance.
[C #] public static delegate CreateDelegate (Type, Object, String);
Create a delegate of the specified type, which indicates the specified static method of the specified class.
[C #] public static delegate CreateDelegate (Type, Type, String);
Create a delegate of the specified type that indicates the specified instance method that the specified class instance call to the specified class instance is required to press the specified case instance. [C #] Public Static Delegate CreateDelegate (Type, Object, String, Bool)
The following example demonstrates the creation of a static method commission, an instance method commission and a dynamic call delegation:
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});
}
}
/*operation result
In Class, String: Hello, Int32: 45
In Instance, String: Hello, INT32: 45
* /