EventArgs

来源:互联网 发布:php json对象转数组 编辑:程序博客网 时间:2024/06/07 06:55
public class CatShoutEventArgs : EventArgs{    private string name;    public String Name    {        get        {            return name;        }        set        {            name = value;        }    }}public class CatArgs{    public string OutPutStr;    private string catName;    public CatArgs(string name)    {        this.catName = name;    }    public delegate void CatShoutEventHandler(object sender, CatShoutEventArgs args);    public event CatShoutEventHandler CatShoutArgs;        public void Shout()    {        this.OutPutStr = string.Format("Hellow Mice,I am {0}. ",catName);        if (CatShoutArgs != null)        {            CatShoutEventArgs e = new CatShoutEventArgs();            e.Name = this.catName;            CatShoutArgs(this, e);        }    }}public class MouseArgs{    public string OutPutStr;    private string mouseName;    public MouseArgs(string name)    {        this.mouseName = name;    }    public void Run(object sender,CatShoutEventArgs args)    {        OutPutStr = string.Format("{0} run!{1} is coming. ", this.mouseName,args.Name);    }}

然后调用输出:

protected void btnArgs_Click(object sender, EventArgs e)    {        CatArgs cat = new CatArgs("Cat");        MouseArgs mouse1 = new MouseArgs("jeff");        MouseArgs mouse2 = new MouseArgs("jack");        //表示将Mouse的Run方法通过实例化委托Cat.CatShoutEventHandler登记到        //Cat的事件CatShout当中。        cat.CatShoutArgs+=new CatArgs.CatShoutEventHandler(mouse1.Run);        cat.CatShoutArgs+=new CatArgs.CatShoutEventHandler(mouse2.Run);        cat.Shout();        string scriptStr = string.Format(@"<script type='text/javascript'>alert('{0}{1}{2}')</script>",cat.OutPutStr,mouse1.OutPutStr,mouse2.OutPutStr);        ClientScript.RegisterStartupScript(typeof(Page), "", scriptStr);    }

原创粉丝点击