要求:委托对象的应用,利用委托创建对象,运用加、减运算,运用GetInvocationList 方法。
实践:
using System;
using System.Collections.Generic;
using System.Text;
namespace Lab3
{
delegate void myDelegate(double param1, double param2);
class DelegateTest
{
public void myAdd(double param1, double param2)
{
double result = param1 + param2;
Console.WriteLine(" I am using Add() !" );
Console.WriteLine(" {0}+{1}={2}",param1,param2,result);
}
public void mySub(double param1, double param2)
{
double result = param1 - param2;
Console.WriteLine(" I am user Sub() !");
Console.WriteLine(" {0}-{1}={2}", param1, param2, result);
}
public void myMul(double param1, double param2)
{
double result = param1 * param2;
Console.WriteLine(" I am usring Mul()!");
Console.WriteLine(" {0}*{1}={2}", param1, param2, result);
}
public void myDiv(double param1, double param2)
{
double result = param1 / param2;
Console.WriteLine(" I am usring Del()!");
Console.WriteLine(" {0}/{1}={2}", param1, param2, result);
}
}
class DelegateEntry
{
static void Main(string[] args)
{
Console.WriteLine(" Delegate tesing begin .....");
DelegateTest delegateTest = new DelegateTest();
myDelegate delegateHandleAdd= new myDelegate(delegateTest.myAdd);
myDelegate delegateHandleSub = new myDelegate(delegateTest.mySub);
myDelegate delegateHandleMul = new myDelegate(delegateTest.myMul);
myDelegate delegateHandleDiv = new myDelegate(delegateTest.myDiv);
if (delegateHandleAdd != null)
{
delegateHandleAdd(5,3);
}
Console.WriteLine("-----------------------------------------------------------------------------");
if (delegateHandleSub != null)
{
delegateHandleSub(5, 3);
}
if (delegateHandleMul != null)
{
delegateHandleMul(5, 3);
}
Console.WriteLine("-----------------------------------------------------------------------------");
if (delegateHandleDiv != null)
{
delegateHandleDiv(5, 3);
}
myDelegate delegateCalculate = null;
delegateCalculate += delegateHandleAdd ;
Console.WriteLine("\n-----------------------------------------------------------------------------");
Console.WriteLine(" I am using [ delegateCalculate += delegateHandleAdd; ]");
delegateCalculate(5, 3);
delegateCalculate += delegateHandleSub;
Console.WriteLine("\n-----------------------------------------------------------------------------");
Console.WriteLine(" I am using [ delegateCalculate += delegateHandleSub; ]");
delegateCalculate(5, 3);
delegateCalculate += delegateHandleMul;
Console.WriteLine("\n-----------------------------------------------------------------------------");
Console.WriteLine(" I am using [ delegateCalculate += delegateHandleMul; ]");
delegateCalculate(5, 3);
delegateCalculate += delegateHandleDiv;
Console.WriteLine("\n-----------------------------------------------------------------------------");
Console.WriteLine(" I am using [ delegateCalculate += delegateHandleDiv; ]");
delegateCalculate(5, 3);
Delegate[] delegateArray = delegateCalculate.GetInvocationList();
Console.WriteLine("\n-----------------------------------------------------------------------------");
Console.WriteLine(" I am using [ delegateCalculate.GetInvocationList(); ]");
Console.WriteLine("\n I have using these methods :\n");
foreach (Delegate delgt in delegateArray)
{
Console.WriteLine(delgt.Method.GetBaseDefinition());
}
delegateCalculate -= delegateHandleAdd;
Console.WriteLine("\n-----------------------------------------------------------------------------");
Console.WriteLine(" I am using [ delegateCalculate -= delegateHandleAdd; ]");
delegateCalculate(5, 3);
delegateCalculate -= delegateHandleSub;
Console.WriteLine("\n-----------------------------------------------------------------------------");
Console.WriteLine(" I am using [ delegateCalculate -= delegateHandleSub; ]");
delegateCalculate(5, 3);
delegateCalculate -= delegateHandleMul;
Console.WriteLine("\n-----------------------------------------------------------------------------");
Console.WriteLine(" I am using [ delegateCalculate -= delegateHandleMul; ]");
delegateCalculate(5, 3);
delegateCalculate -= delegateHandleDiv;
Console.WriteLine("\n-----------------------------------------------------------------------------");
Console.WriteLine(" I am using [ delegateCalculate -= delegateHandleDiv; ]");
Console.WriteLine(" Delegate tesing end .....");
Console.ReadLine();
}
}
}