//调用方法,用一个object接收返回值
object   returnValue   =   method.Invoke(dObj,flag,Type.DefaultBinder,parameters,null);
属性与方法的调用大同小异,大家也可以参考MSDN

7、动态创建委托
    委托是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(t,obj,”GetValue”);
String   returnValue   =   method(“hello”);

到这里,我们简单的讲述了反射的作用以及一些基本的用法,还有很多方面没有涉及到,有兴趣的朋友可以参考MSDN。
   很奇怪,很多人都不愿看MSDN,其实你想要的答案,99%都可以在里面找到。

相关文章:

  • 2021-06-19
  • 2021-07-07
  • 2021-09-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-07
猜你喜欢
  • 2021-06-17
  • 2021-11-22
  • 2022-01-08
  • 2021-06-02
  • 2021-07-17
  • 2022-01-12
相关资源
相似解决方案