举例:
Type tDate = typeof(System.DateTime);
Object result = tDate.InvokeMember("Now",
BindingFlags.GetProperty, null, null, new Object[]);
Console.WriteLine(result.ToString()); 例2: /*注意:下面备注的方法都是其他类的方法,比如:TestClass类方法*/
TestClass tc = new TestClass (); //AddUp为tc的方法
tc.GetType().InvokeMember ("AddUp", BindingFlags.Public |
BindingFlags.Instance | BindingFlags.CreateInstance,
null, tc, new object [] {});
/*
public void AddUp ()
{
methodCalled++;
Console.WriteLine ("AddUp Called {0} times", methodCalled);
}
*/
//----------------------------下面传参数 执行ComputeSum方法 带有两个参数
Type t = typeof (TestClass);
object [] args = new object [] {100.09, 184.45};
object result = t.InvokeMember ("ComputeSum", BindingFlags.InvokeMethod, null, null, args);
/*
public static double ComputeSum (double d1, double d2)
{
return d1 + d2;
}
*/
//-----------SayHello为静态方法调用
t.InvokeMember ("SayHello", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static, null, null, new object [] {});
//-----------实例方法调用
TestClass c = new TestClass ();
c.GetType().InvokeMember ("AddUp", BindingFlags.InvokeMethod, null, c, new object [] {}); c.GetType().InvokeMember ("AddUp", BindingFlags.Public |
BindingFlags.Instance | BindingFlags.CreateInstance,
null, c, new object [] {}); //----------获取字段
result = t.InvokeMember ("Name", BindingFlags.GetField | BindingFlags.GetProperty, null, c, new object [] {});
result = t.InvokeMember ("Value", BindingFlags.GetField | BindingFlags.GetProperty, null, c, new object [] {});
/*
public String Name;
public Object Value
{
get
{
return "the value";
}
}
*/
result = t.InvokeMember ("Name", BindingFlags.GetField, null, c, new object [] {});
//---------设置字段
t.InvokeMember ("Name", BindingFlags.SetField, null, c, new object [] {"NewName"}); //NewName设置的属性值
//---------调用类方法 带参数
object[] argValues = new object [] {"Mouse", "Micky"};
String [] argNames = new String [] {"lastName", "firstName"};
t.InvokeMember ("PrintName", BindingFlags.InvokeMethod, null, null, argValues, null, null, argNames);
/*
public static void PrintName (String firstName, String lastName)
{
Console.WriteLine ("{0},{1}", lastName,firstName);
}
*/
TestClass obj = new TestClass();
System.Reflection.MethodInfo methInfo =
obj.GetType().GetMethod("PrintName");
methInfo.Invoke(obj,BindingFlags.SuppressChangeType |
BindingFlags.InvokeMethod, null,new object[]
{"Brad","Smith"},null); methInfo = obj.GetType().GetMethod("PrintName");
methInfo.Invoke(obj,BindingFlags.IgnoreCase | //忽略大小写 指定当绑定时不应考虑成员名的大小写
BindingFlags.InvokeMethod, null,new object[]
{"brad","smith"},null); methInfo = obj.GetType().GetMethod("PrintName");
methInfo.Invoke(obj,BindingFlags.IgnoreReturn | // 在 COM interop 中用于指定可以忽略成员的返回值
BindingFlags.InvokeMethod, null,new object[]
{"Brad","Smith"},null); methInfo = obj.GetType().GetMethod("PrintName");
methInfo.Invoke(obj,BindingFlags.OptionalParamBinding |
BindingFlags.InvokeMethod, null,new object[]
{"Brad","Smith"},null); // BindingFlags.ExactBinding
methInfo = obj.GetType().GetMethod("PrintName");
methInfo.Invoke(obj,BindingFlags.ExactBinding |
BindingFlags.InvokeMethod, null,new object[]
{"Brad","Smith"},null); // BindingFlags.FlattenHierarchy
methInfo = obj.GetType().GetMethod("PrintName");
methInfo.Invoke(obj,BindingFlags.FlattenHierarchy |
BindingFlags.InvokeMethod, null,new object[]
{"Brad","Smith"},null);
//----------调用一个默认的方法
Type t3 = typeof (TestClass2);
/*
[DefaultMemberAttribute ("PrintTime")]
public class TestClass2
{
public void PrintTime ()
{
Console.WriteLine (DateTime.Now);
}
}
*/
t3.InvokeMember ("", BindingFlags.InvokeMethod | BindingFlags.Default, null, new TestClass2(), new object [] {});
//---------调用一个引用方法
MethodInfo m = t.GetMethod("Swap");
args = new object[];
args[] = ;
args[] = ;
m.Invoke(new TestClass(),args);
/*
public void Swap(ref int a, ref int b) 交换 a b
{
int x = a;
a = b;
b = x;
}
*/

Type InvokeMember()用法简介的更多相关文章

  1. c# Type.InvokeMember用法

    函数原型: public object InvokeMember(string, BindingFlags, Binder, object, object[]);string:你所要调用的函数名Bin ...

  2. 【C#反射】Type的用法

    Type属性的应用 Type type = typeof(MyClass); Console.Write("$类型名:{ type.Name}"); Console.Write(& ...

  3. Postman用法简介

    转自:http://blog.csdn.net/flowerspring/article/details/52774399 Postman用法简介 转载 2016年10月10日 09:04:10 10 ...

  4. [转帖]linux lsof 用法简介

    linux lsof 用法简介 https://www.cnblogs.com/saneri/p/5333333.html 1.简介: lsof(list open files)是一个列出当前系统打开 ...

  5. C++ Json工具--Jsoncpp用法简介

    文章目录 Json简介 用法简介 数据类型 C++代码示例 代码执行输出结果 JSON在线解析及格式化验证 - JSON.cn Json简介 JSON(JavaScript Object Notati ...

  6. IOS NSInvocation用法简介

    IOS NSInvocation用法简介 2012-10-25 19:59 来源:博客园 作者:csj007523 字号:T|T [摘要]在 iOS中可以直接调用某个对象的消息方式有两种,其中一种就是 ...

  7. JodaTime用法简介

    JodaTime用法简介 Java的Date和Calendar用起来简直就是灾难,跟C#的DateTime差距太明显了,幸好有JodaTime 本文简单罗列JodaTime的用法 package co ...

  8. Activator.CreateInstance 方法 (Type) 的用法

    转自:http://www.cnblogs.com/lmfeng/archive/2012/01/30/2331666.html Activator.CreateInstance 方法 (Type) ...

  9. Apache自带压力测试工具ab用法简介

    ab命令原理 ab命令会创建很多的并发访问线程,模拟多个访问者同时对某一URL进行访问.它的测试目标是基于URL的,因此,既可以用来测试Apache的负载压力,也可以测试nginx.lighthttp ...

随机推荐

  1. 初学JDBC,最简单示例

    一.下载相应数据库驱动jar包,添加到项目中 二.注册驱动,数据库驱动只加入到classpath中是还不行的,还要在使用的时候注册一下,就像安装驱动软件,只拷贝到硬盘还不行,需要安装一下 Driver ...

  2. CSS transition 过渡 详解

    transition 过渡 IE10.Firefox.Chrome.Opera 支持 transition 属性. Safari 需要前缀 -webkit-. Chrome 25 以及更早版本需要前缀 ...

  3. Java操作Excel(读、写、搜索关键字、插入图片)

    import java.io.File; import java.io.IOException; import jxl.Cell; import jxl.Sheet; import jxl.Workb ...

  4. ASP.NET MVC4中使用Ninject

    1.NuGet获取Ninject.dll 选中项目右键: .NET技术交流群 199281001 .欢迎加入. 2.全局注册  Global.asax.cs //注册Ninject依赖注入全局解析器 ...

  5. gvim-ide plugins

    omnicppcomplete-0.41.zip after: ftplugin: c.vim,cpp.vim,两个文件的内容相同, 其中都只有一个call语句: call omni#cpp#comp ...

  6. 每日一练(写不出心得体会了!毕竟哪有那么多心得好写。然后看github上有很多不错的题目。分享一下!)

    第一题: 问题描述:写一个reverseWords函数 调用方式:console.log(reverseWords('Hello World')); 期望输出:World Hello 第二题: 问题描 ...

  7. iOS设备屏幕像素总览

    本文永久地址为http://www.cnblogs.com/ChenYilong/p/4011728.html ,转载请注明出处.   本文永久地址为http://www.cnblogs.com/Ch ...

  8. JavaScript中,window.opener是什么?window.parent和window.opener有啥区别?

    来自CSDN的问答: window.opener是什么啊? ++++++++++++++++++++++++++++++++++++++++++++++++++ 弹出本窗体的句柄 比如你想点一个按钮直 ...

  9. Hibernate执行原生SQL返回List<Map>类型结果集

    我是学java出身的,web是我主要一块: 在做项目的时候最让人别扭的就是hibernate查询大都是查询出List<T>(T指代对应实体类)类型 如果这时候我用的联合查询,那么返回都就是 ...

  10. [Educational Codeforces Round 16]D. Two Arithmetic Progressions

    [Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...