zoukankan      html  css  js  c++  java
  • 利用反射绑定事件处理程序(C#)

    利用反射绑定事件处理程序(C#)

    传统的写法:强类型的情况下

    using System;
    using System.Collections.Generic;
    using System.Text;

    using System.Reflection;

    namespace ConsoleApplication3
    {
    class Program
    {
    static void Main(string[] args)
    {
    Customer c = new Customer();
    c.OnChange += new EventHandler(c_OnChange);

    c.Change();
    Console.Read();

    }

    static void c_OnChange(object sender, EventArgs e)
    {
    Console.WriteLine("事件被触发了");
    }
    }

    class Customer
    {
    public event EventHandler OnChange;
    public void Change()
    {
    if (OnChange != null)
    OnChange(this, null);
    }
    }
    }

    值得了解的另外一个写法

    using System;
    using System.Collections.Generic;
    using System.Text;

    using System.Reflection;

    namespace ConsoleApplication3
    {
    class Program
    {
    static void Main(string[] args)
    {
    Customer c = new Customer();
    EventInfo evt = c.GetType().GetEvent("OnChange",
    BindingFlags.NonPublic | BindingFlags.Instance
    | BindingFlags.Public
    );

    evt.AddEventHandler(c, new EventHandler(c_OnChange));

    c.Change();
    Console.Read();

    }

    static void c_OnChange(object sender, EventArgs e)
    {
    Console.WriteLine("事件被触发了");
    }
    }

    class Customer
    {
    public event EventHandler OnChange;
    public void Change()
    {
    if (OnChange != null)
    OnChange(this, null);
    }
    }
    }

    传统的写法:强类型的情况下

    using System;
    using System.Collections.Generic;
    using System.Text;

    using System.Reflection;

    namespace ConsoleApplication3
    {
    class Program
    {
    static void Main(string[] args)
    {
    Customer c = new Customer();
    c.OnChange += new EventHandler(c_OnChange);

    c.Change();
    Console.Read();

    }

    static void c_OnChange(object sender, EventArgs e)
    {
    Console.WriteLine("事件被触发了");
    }
    }

    class Customer
    {
    public event EventHandler OnChange;
    public void Change()
    {
    if (OnChange != null)
    OnChange(this, null);
    }
    }
    }

    值得了解的另外一个写法

    using System;
    using System.Collections.Generic;
    using System.Text;

    using System.Reflection;

    namespace ConsoleApplication3
    {
    class Program
    {
    static void Main(string[] args)
    {
    Customer c = new Customer();
    EventInfo evt = c.GetType().GetEvent("OnChange",
    BindingFlags.NonPublic | BindingFlags.Instance
    | BindingFlags.Public
    );

    evt.AddEventHandler(c, new EventHandler(c_OnChange));

    c.Change();
    Console.Read();

    }

    static void c_OnChange(object sender, EventArgs e)
    {
    Console.WriteLine("事件被触发了");
    }
    }

    class Customer
    {
    public event EventHandler OnChange;
    public void Change()
    {
    if (OnChange != null)
    OnChange(this, null);
    }
    }
    }

  • 相关阅读:
    人工智能 有信息搜索 (启发式)
    python学习之高阶函数
    文件学习
    codeblock快捷键使用
    React项目之BrowserRouter路由方式之-------生产环境404问题
    React生产环境打包&&后台环境运行(有跨域+无跨域)
    React前台改用HashRouter并解决两个问题
    React路由基础
    React前台404组件页面+路由控制重定向
    react调用方法
  • 原文地址:https://www.cnblogs.com/dlbird/p/3974597.html
Copyright © 2011-2022 走看看