函数原型:   
    public   object   InvokeMember(string,   BindingFlags,   Binder,   object,   object[]);   
  string:你所要调用的函数名   
  BindingFlags:你所要调用的函数的属性,可以组合   
  Binder:高级内容,可以先不看   
  object:调用该成员函数的实例   
  object[]:参数,   
  下面是msdn例子:   
  //调用公有静态成员函数(不带参数)   
    Type   t   =   typeof   (TestClass);   
    t.InvokeMember   ("SayHello",   BindingFlags.Public   |   BindingFlags.InvokeMethod   |   BindingFlags.Static,   null,   null,   new   object   []   {});   
                      
    //调用实例的函数(不带参数),第三个参数为该实例   
    TestClass   c   =   new   TestClass   ();   
    c.GetType().InvokeMember   ("AddUp",   BindingFlags.Public   |   BindingFlags.InvokeMethod,   null,   c,   new   object   []   {});   
    c.GetType().InvokeMember   ("AddUp",   BindingFlags.Public   |   BindingFlags.InvokeMethod,   null,   c,   new   object   []   {});   
                      
  //调用带参数的函数,   
  //方法是:将你的所有参数都放到一个object的数组里面   
    object   []   args   =   new   object   []   {100.09,   184.45};   
    object   result;   
    result   =   t.InvokeMember   ("ComputeSum",   BindingFlags.Public   |   BindingFlags.InvokeMethod   |   BindingFlags.Static,   null,   null,   args);   
    Console.WriteLine   ("{0}   +   {1}   =   {2}",   args[0],   args[1],   result);   
                      
    //获得一个属性值   
    result   =   t.InvokeMember   ("Name",   BindingFlags.Public   |   BindingFlags.GetField,   null,   c,   new   object   []   {});   
    Console.WriteLine   ("Name   ==   {0}",   result);   
                      
    //设定一个属性值   
    t.InvokeMember   ("Name",   BindingFlags.Public   |BindingFlags.SetField,   null,   c,   new   object   []   {"NewName"});   
    result   =   t.InvokeMember   ("Name",   BindingFlags.Public   |BindingFlags.GetField,   null,   c,   new   object   []   {});   
    Console.WriteLine   ("Name   ==   {0}",   result);   
                      
    //获得一个下标属性值([])   
    int     index   =   3;   
    result   =   t.InvokeMember   ("Item",   BindingFlags.Public   |BindingFlags.GetProperty   ,   null,   c,   new   object   []   {index});   
    Console.WriteLine   ("Item[{0}]   ==   {1}",   index,   result);   
                      
    //设定一个下标属性值([])     
    index   =   3;   
    t.InvokeMember   ("Item",   BindingFlags.Public   |BindingFlags.SetProperty,   null,   c,   new   object   []   {index,   "NewValue"});   
    result   =   t.InvokeMember   ("Item",   BindingFlags.Public   |BindingFlags.GetProperty   ,   null,   c,   new   object   []   {index});   
    Console.WriteLine   ("Item[{0}]   ==   {1}",   index,   result);   
                      
    //获得一个属性或者是成员变量的值   
  //也就是,假设有一个类是这样的:   
  //class   temp{   
  //   public   string   name;     
  //   public   string   Name{     
  //         get{return   name;}   
  //         set   {name=value}   
  //   }   
  //}   
  //那么通过一下语句就可获得Name的值,   
    result   =   t.InvokeMember   ("Name",   BindingFlags.Public   |BindingFlags.GetField   |   BindingFlags.GetProperty,   null,   c,   new   object   []   {});   
    Console.WriteLine   ("Name   ==   {0}",   result);   
  //通过一下,语句可以获得name的值   
    result   =   t.InvokeMember   ("name",   BindingFlags.Public   |BindingFlags.GetField   |   BindingFlags.GetProperty,   null,   c,   new   object   []   {});   
    Console.WriteLine   ("Value   ==   {0}",   result);   
                      
    //调用一个函数,使用参数名对应的参数   
    object[]   argValues   =   new   object   []   {"Mouse",   "Micky"};   
    String   []   argNames   =   new   String   []   {"lastName",   "firstName"};   
    t.InvokeMember   ("PrintName",   BindingFlags.Public   |BindingFlags.InvokeMethod,   null,   null,   argValues,   null,   null,   argNames);   
                      
    //调用一个类型的默认函数,好像在C#里面没有默认成员函数   
    Type   t3   =   typeof   (TestClass2);   
    t3.InvokeMember   ("",   BindingFlags.Public   |BindingFlags.InvokeMethod,   null,   new   TestClass2(),   new   object   []   {});   
                      
    //Invoking   a   ByRef   member   
    MethodInfo   m   =   t.GetMethod("Swap");   
    args   =   new   object[2];   
    args[0]   =   1;   
    args[1]   =   2;   
    m.Invoke(new   TestClass(),args);

最新文章

  1. 获取上个页面的url包括参数
  2. Hover.css:一组超实用的 CSS3 悬停效果和动画
  3. NSClassFromString 和 遍历UIView获取她所在的UIViewController的tips
  4. asp.net mvc4 设置build项目时,编译view页面
  5. Python开发【第一篇】Python基础之自定义模块和内置模块
  6. Linux下eclipse的安装以及配置
  7. seajs常用API整理
  8. JAVA finally字句的异常丢失和返回值覆盖解析
  9. Web服务器自定义错误页面
  10. URL模块之parse方法
  11. 实验五:任意输入10个int类型数据,排序输出,并找出素数
  12. 准备PPT过程中的一些文档记录
  13. 【C++】static关键字的总结
  14. 在Linux环境下使用Jexus部署ASP.NET Core
  15. 动态代理Dynamic Proxy
  16. SVN Hooks的介绍及使用
  17. 最简单的例子理解Javascript闭包
  18. Android: 背景图片平铺要这么干
  19. yii2 restful api --app接口编程
  20. [CTSC 2018]假面

热门文章

  1. 下载文件的Restful接口的前端实现
  2. spark调优——算子调优
  3. es6 函数解构的用途
  4. Centrifugo  语言无关的实时消息服务
  5. Xamarin NuGet 缓存包导致 already added : Landroid/support/annotation/AnimRes 问题解决方案
  6. js时间练习
  7. K8s Service原理介绍
  8. ssl 原理简介
  9. [已解决] Python logging 重复打印日志信息
  10. docker之网络桥接的两种方式