函数原型:   
    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);

InvokeMember 使用(转http://blog.csdn.net/gooer/article/details/2927113)的更多相关文章

  1. http://blog.csdn.net/java2000_wl/article/details/8627874

    http://blog.csdn.net/java2000_wl/article/details/8627874

  2. android 蓝牙 http://blog.csdn.net/u012843100/article/details/52384219

    http://blog.csdn.net/u012843100/article/details/52384219

  3. http://blog.csdn.net/krislight/article/details/9391455

    http://blog.csdn.net/krislight/article/details/9391455

  4. http://blog.csdn.net/shawnkong/article/details/52045894

    http://blog.csdn.net/shawnkong/article/details/52045894

  5. 使用C++扩展Python的功能 转自:http://blog.csdn.net/magictong/article/details/8897568#comments

    使用C++扩展Python的功能 环境 VS2005Python2.5.4 Windows7(32位) 简介 长话短说,这里说的扩展Python功能与直接用其它语言写一个动态链接库,然后让Python ...

  6. http://blog.csdn.net/hitmediaman/article/details/6636402

    http://blog.csdn.net/hitmediaman/article/details/6636402

  7. http://blog.csdn.net/iamshaofa/article/details/7877785/

    http://blog.csdn.net/iamshaofa/article/details/7877785/

  8. http://blog.csdn.net/z69183787/article/details/37819831

    http://blog.csdn.net/z69183787/article/details/37819831

  9. http://blog.csdn.net/u010246789/article/details/52539576

    http://blog.csdn.net/u010246789/article/details/52539576

随机推荐

  1. Sitemap Error : XML declaration allowed only at the start of the document解决方法

    今天ytkah的客户反馈说他的xml网站地图有问题,提示Sitemap Error : XML declaration allowed only at the start of the documen ...

  2. Spring动态切换多数据源事务开启后,动态数据源切换失效解决方案

    关于某操作中开启事务后,动态切换数据源机制失效的问题,暂时想到一个取巧的方法,在Spring声明式事务配置中,可对不改变数据库数据的方法采用不支持事务的配置,如下: 对单纯查询数据的操作设置为不支持事 ...

  3. ssh配置连接远程主机 彻底解放你的双手

    查看ssh支持配置 man ssh_config 打开ssh并配置 vi ~/.ssh/config 基本配置示例说明 密钥文件连接 Host <别名> Port <机器端口号> ...

  4. [RN] React Native 使用开源库 react-native-image-crop-picker 实现图片选择、图片剪裁

    React Native 使用开源库 react-native-image-crop-picker 实现图片选择.图片剪裁 该库可以实现启动本地相册和照相机来采集图片,并且提供多选.图片裁剪等功能,支 ...

  5. python实现余弦相似度文本比较

    向量空间模型VSM: VSM的介绍: 一个文档可以由文档中的一系列关键词组成,而VSM则是用这些关键词的向量组成一篇文档,其中的每个分量代表词项在文档中的相对重要性. VSM的例子: 比如说,一个文档 ...

  6. uni app 零基础小白到项目实战

    $emit 子组件传给父组件 $ref 父组件操作子组件 公用模板 uni-app全局变量的几种实现方法 const websiteUrl = 'http' const now = Date.now ...

  7. [golang][gui]Hands On GUI Application Development in Go【在Go中动手进行GUI应用程序开发】读书笔记03-拒交“智商税”,解密“GUI”运行之道

    和老外的原文好像没多大联系了,哈哈哈,反正是读书笔记,下面的内容也是我读此书中的历程,也写进来吧.不过说实话,这框架的作者还挺对我脾气的,哈哈哈. 拒交“智商税”,解密“GUI”运行之道 我很忙 项目 ...

  8. java自动化配置工具 - autoconfig 简介

    对于java程序员来说各种各样的配置文件是司空见惯的,比如spring的bean配置,struts的action配置等等.有些配置会随着运行环境的变化而各不相同,最典型的就是jdbc驱动的配置,在开发 ...

  9. shell 查找字符串中字符出现的位置

    #!/bin/bash a="The cat sat on the mat" test="cat" awk -v a="$a" -v b=& ...

  10. HustOJ二次开发之修改相关Logo

    比如将如图中的HUSTOJ进行修改: 在Linux上修改,通过关键字搜索,会获取如下两个重要文件,找到都有的文字进行修改即可: grep -rn "HUSTOJ" * cd /ho ...