using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace AspNetTest.Common
{
/// <summary>
/// Define_Delegate_handwork 的摘要说明。
/// </summary>
public class Define_Delegate_handwork : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
MathOp mathOp = new MathOp(1,0);
mathOp.opDelegate = null;
mathOp.opDelegate += new MathOp.OpDelegate(OperateFunc.Add);
mathOp.opDelegate += new MathOp.OpDelegate(OperateFunc.Subtract);
mathOp.opDelegate += new MathOp.OpDelegate(OperateFunc.Divide);
mathOp.opDelegate += new MathOp.OpDelegate(OperateFunc.Multiply);
object[] args = new object[2];
args[0] = mathOp.Op1;
args[1] = mathOp.Op2;
Delegate[] operaters = mathOp.opDelegate.GetInvocationList();
foreach(Delegate operater in operaters)
{
try
{
operater.DynamicInvoke(args);
}
catch(Exception ex)
{
Response.Write("<font color=red>被零除,引发一个异常,但并不影响委托链的其他实例运行!</font><br>" + "异常信息:<font color=gray>" + ex.Message + "</font><br><br>");
}
}
}
public class MathOp
{
public MathOp(int op1, int op2)
{
this.op1 = op1;
this.op2 = op2;
}
public delegate void OpDelegate(int op1, int op2);
public OpDelegate opDelegate;
private int op1;
private int op2;
public int Op1
{
get
{
return this.op1;
}
}
public int Op2
{
get
{
return this.op2;
}
}
}
public class OperateFunc
{
public static void Add(int op1, int op2)
{
int result = (int)op1+op2;
HttpContext.Current.Response.Write("加:" + result + "<br>");
}
public static void Subtract(int op1, int op2)
{
int result = (int)op1-op2;
HttpContext.Current.Response.Write("减:" + result + "<br>");
}
public static void Multiply(int op1, int op2)
{
int result = (int)op1*op2;
HttpContext.Current.Response.Write("乘:" + result + "<br>");
}
//表示整除
public static void Divide(int op1, int op2)
{
int result = (int)op1/op2;
//注释该行,故意引发异常
//return op2==0 ? 0 : (int)op1/op2;
HttpContext.Current.Response.Write("除:" + result + "<br>");
}
}
Web 窗体设计器生成的代码
}
}


浙公网安备 33010602011771号