Lab requirements
1. select an application instance, which can be an example in the teaching material or a completed experiment design. Declare a custom delegate type for the selected example and use this delegate type, and call the delegate type in the program.
2. The program to be implemented must use the operator +,-operation delegate object to verify the basic features of the delegate type + and-operation.
3. You must use the getinvocationlist method to obtain information in the delegate object's representative list.
A) determine whether a delegated object exists.
B) Use the return value of the getinvocationlist method to perform operations (select) on the delegate object Association processing function ).
Using system;
Namespace sei. dl88250.sourcecodes. CSHARP
{
Public Delegate void calculation (decimal val1,
Decimal val2,
Ref decimal result );
Class multicastdelegate
{
Calculation myadd;
Calculation mysub;
Calculation mymul;
Public void add (decimal Add1, decimal Add2, ref decimal result)
{
Result = Add1 + Add2;
Console. writeline ("{0} + {1} = {2 }",
Add1, Add2, result );
}
Public void sub (decimal sub1, decimal sub2, ref decimal result)
{
Result = sub1-sub2;
Console. writeline ("{0}-{1} = {2 }",
Sub1, sub2, result );
}
Public void MUL (decimal mul1, decimal mul2, ref decimal result)
{
Result = mul1 * mul2;
Console. writeline ("{0} * {1} = {2 }",
Mul1, mul2, result );
}
Static void main (string [] ARGs)
{
Decimal result = 0.0 m;
Multicastdelegate MCD = new multicastdelegate ();
MCD. myadd = new calculation (MCD. Add );
MCD. mysub = new calculation (MCD. sub );
MCD. mymul = new calculation (MCD. Mul );
Console. foregroundcolor = consolecolor. darkred;
Console. writeline ("use single delegate :");
Console. foregroundcolor = consolecolor. darkgray;
MCD. myadd (7.43 M, 5.19 M, ref result );
MCD. mysub (7.43 M, 5.19 M, ref result );
MCD. mymul (7.43 M, 5.19 M, ref result );
Console. foregroundcolor = consolecolor. darkred;
Console. writeline ("use multicast delegate :");
Console. foregroundcolor = consolecolor. darkgray;
Calculation multic1c = McD. myadd + McD. mysub + McD. mymul;
Multic1c (7.43 M, 5.19 M, ref result );
Console. foregroundcolor = consolecolor. darkred;
Console. writeline ("Remove the sub method delegate :");
Console. foregroundcolor = consolecolor. darkgray;
Multic1c-= McD. mysub;
Multic1c (7.43 M, 5.19 M, ref result );
// Restore delegate sub Method
Multic1c + = McD. mysub;
Console. foregroundcolor = consolecolor. darkred;
Console. writeline ("delegate contents :");
Console. foregroundcolor = consolecolor. darkgray;
Delegate [] delegatearray = multic1c. getinvocationlist ();
Foreach (delegate delgt in delegatearray)
{
Console. writeline (delgt. method. getbasedefinition ());
}
Console. foregroundcolor = consolecolor. White;
}
}
}