zoukankan      html  css  js  c++  java
  • 动态的委托方法

    委托是C#中实现事件的基础,有时候不可避免的要动态的创建委托,实际上委托也是一种类型:System.Delegate,所有的委托都是从这个类派生的
    System.Delegate提供了一些静态方法来动态创建一个委托,比如一个委托:

    namespace TestSpace {
       delegate string TestDelegate(string value);
       public class TestClass {
     public TestClass() {
             }
             public void GetValue(string value) {
                 return value;
             }
        }
    }


    使用示例:
    TestClass obj = new TestClass();
      
    //获取类型,实际上这里也可以直接用typeof来获取类型
    Type t = Type.GetType(“TestSpace.TestClass”);
    //创建代理,传入类型、创建代理的对象以及方法名称
    TestDelegate method = (TestDelegate)Delegate.CreateDelegate(typeof(Delegate),obj,”GetValue”);

    String returnValue = method(“hello”);

    如果想委托页面方法:

        protected void Page_Load(object sender, EventArgs e)
        {
            EventHandler method = (EventHandler)EventHandler.CreateDelegate(typeof(EventHandler), this, "hello");
            EventArgs e1 = new EventArgs();
            method(null, e1);

        }

        public void hello(object sender, EventArgs e)
        {
          
        }

  • 相关阅读:
    图解测试之稳定性-如何开始稳定性测试
    系统稳定性保障
    系统稳定性评测
    分布式架构的架构稳定性
    app测试--稳定性测试
    服务器稳定性测试方法汇总
    服务端稳定性测试
    发票问题
    android x86 固件定制
    Nim游戏博弈(收集完全版)
  • 原文地址:https://www.cnblogs.com/cuihongyu3503319/p/1268739.html
Copyright © 2011-2022 走看看