using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection; namespace MethodInfoInvokeDemo
{
public class ReflectTest
{
public void MethodWithNoParaNoReturn()
{
Console.WriteLine("不带参数且不返回值的方法");
} public string MethodWithNoPara()
{
Console.WriteLine("不带参数且有返回值的方法");
return "MethodWithNoPara";
} public string Method1(string str)
{
Console.WriteLine("带参数且有返回值的方法");
return str;
} public string Method2(string str, int index)
{
Console.WriteLine("带参数且有返回值的方法");
return str + index.ToString();
} public string Method3(string str, out string outStr)
{
outStr = "bbbb";
Console.WriteLine("带参数且有返回值的方法");
return str;
} public static string StaticMethod()
{
Console.WriteLine("静态方法");
return "cccc";
}
} class Program
{
static void Main(string[] args)
{
Type type = typeof(ReflectTest);
object reflectTest = Activator.CreateInstance(type); //不带参数且不返回值的方法的调用
MethodInfo methodInfo = type.GetMethod("MethodWithNoParaNoReturn");
methodInfo.Invoke(reflectTest, null); Console.WriteLine(); //不带参数且有返回值的方法的调用
methodInfo = type.GetMethod("MethodWithNoPara");
Console.WriteLine(methodInfo.Invoke(reflectTest, null).ToString()); Console.WriteLine(); //带参数且有返回值的方法的调用
methodInfo = type.GetMethod("Method1", new Type[]{typeof(string)});
Console.WriteLine(methodInfo.Invoke(reflectTest, new object[]{"测试"}).ToString()); Console.WriteLine(); //带多个参数且有返回值的方法的调用
methodInfo = type.GetMethod("Method2", new Type[] { typeof(string), typeof(int) });
Console.WriteLine(methodInfo.Invoke(reflectTest, new object[] { "测试", 100 }).ToString()); //Console.WriteLine(); //methodInfo = type.GetMethod("Method3", new Type[] { typeof(string), typeof(string) });
//string outStr = "";
//Console.WriteLine(methodInfo.Invoke(reflectTest, new object[] { "测试", outStr }).ToString()); Console.WriteLine(); //静态方法的调用
methodInfo = type.GetMethod("StaticMethod");
Console.WriteLine(methodInfo.Invoke(null, null).ToString()); Console.ReadKey();
}
}
}

reflact中GetMethod方法的使用的更多相关文章

  1. C# 与.NET2.0 中类型Type的GetMethod方法

    C#中类型Type有个GetMethod方法,调用该方法可获取指定方法名的方法信息实例. 使用时,其参数一般为2个,一个是方法名称字符串(可设置条件忽略大小写),另外一个参数为搜索方法的条件枚举. 该 ...

  2. java 27 - 6 反射之 通过配置文件运行类中的方法

    在以前,如果我们想要调用一个类中的方法,只能这样子: 例: 有Cat和Dog两个类,里面有eat和run两个成员方法: public class Dog { public void eat() { S ...

  3. InvocationHandler中invoke()方法的调用问题

    转InvocationHandler中invoke()方法的调用问题 Java中动态代理的实现,关键就是这两个东西:Proxy.InvocationHandler,下面从InvocationHandl ...

  4. js调DLL类库中的方法实现(非com组件形式)

    1.首先,创建一个Web空项目 2.添加一个html或aspx页面 3.页面代码如所示: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tr ...

  5. 动态的调用服务端的WCF中的方法

    客户端调用wcf ,有时需要动态的调用服务端的WCF中的方法,本方法,反射wcf 的接口,动态调用接口中的方法. 主要为,动态绑定,反射动态调用. public static object Execu ...

  6. Java反射机制demo(五)—获得并调用一个类中的方法

    Java反射机制demo(五)—获得并调用一个类中的方法 这个demo在使用反射机制操作属性之前,主要原因是因为在.class文件字节码中,方法排在属性的前面. 1,获得一个类中的方法 先看一下方法和 ...

  7. 6、获取Class中的方法

    6.获取Class中的方法 6.1 getMethods() 获取的都是共有的方法(包括父类) 返回包含一个数组 方法对象反射由此表示的类或接口的所有公共方法 类对象,包括那些由类或接口和那些从超类和 ...

  8. 反射实现定位Servlet中的方法

    public class BaseServlet extends HttpServlet{ @Override protected void service(HttpServletRequest re ...

  9. category中重写方法?

    问:可以在category中重写方法吗? 答:代码上可以实现 在category中重写方法,但在实际开发中,不建议这样做.如果确实需要重写原有方法也建议使用子类进行重写. category是为了更方便 ...

随机推荐

  1. ant 学习与开发

    ant 学习与开发 http://www.blogjava.net/amigoxie/archive/2007/11/09/159413.html

  2. Delphi实现WebService带身份认证的数据传输

    WebService使得不同开发工具开发出来的程序可以在网络连通的环境下相互通信,它最大的特点就是标准化(基于XML的一系列标准)带来的跨平台.跨开发工具的通用性,基于HTTP带来的畅通无阻的能力(跨 ...

  3. HTMLTestRunner生成空白resault.html

    发现一奇葩问题,用idle或pyscripter执行脚本,生成的是空白html,通过cmd,进入脚本目录执行python xx.py,却能生成测试报告. HTMLTestRunner 例子 #codi ...

  4. ubuntu下使用ngrok外网映射

    好久之前想搞明白这个事情,可是就是不知道这个词叫外网映射,所以也一直不知怎么做,在慕课网看用java开发微信公众号的时候教程里提到了外网映射,查了一些资料终于把本地给映射到外网了,直接变成了80端口, ...

  5. cocos2d的安装

    安装cocos2d其实就是在Xcode中安装几个模板,然后在Xcode里面就可以直接使用这些模板了. 其实说是模板,也就是封装了许许多多引擎的文件,相对于原生的程序,也许使用引擎模板更加方便.   下 ...

  6. 开源跨平台的3D渲染软件

    http://www.blender.org/ KVM是Kernel-based Virtual Machine的缩写; http://kiwik.github.io/openstack/2013/1 ...

  7. Django中的cookie与session

    cookie与session的实现原理 HTTP被设计为”无状态”,每次请求都处于相同的空间中. 在一次请求和下一次请求之间没有任何状态保持,我们无法根据请求的任何方面(IP地址,用户代理等)来识别来 ...

  8. reids-geo

    Redis Geo Geo

  9. [iOS] iOS系统中各种设置项的url链接

    在代码中调用如下代码:NSURL*url=[NSURL URLWithString:@"prefs:root=WIFI"];[[UIApplication sharedApplic ...

  10. 字符串、十六进制、byte数组互转

    import java.io.ByteArrayOutputStream; public class HexUtil { /** * @param args */ public static void ...