Reflect中MethodInfo使用方法
来源:互联网 发布:申宏源证券软件下载 编辑:程序博客网 时间:2024/05/13 14:03
Reflect中MethodInfo使用方法
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();
}
}
}
- Reflect中MethodInfo使用方法
- Reflect中MethodInfo使用方法
- C#中typeof 与GetType()的区别和methodinfo、memberinfo反射
- golang 中 reflect 简单用法
- Java中Reflect反射小结
- reflect
- reflect
- Reflect
- Reflect
- Reflect
- Reflect
- Reflect
- Reflect
- Reflect
- reflect
- Reflect
- reflect
- reflect
- PHP is_null
- 飞机售票系统
- 软键盘的弹出的操作
- 关于C语言指针的理解
- nil Nil null @"" 关于nil和 null和NSNull的区别及相关问题||(null) <null> nil 问题
- Reflect中MethodInfo使用方法
- js 全屏
- 驱动和应用层的三种通信方式
- sql 添加 修改 删除 约束
- JDK的安装与配置
- 在iOS6机子上运行 React-Native Demo, 自己编译 JavaScriptCore 源码,支持 iOS5.1 ,iOS 6
- Docker technical notes
- 用ImDisk在Windows 10中创建内存虚拟盘
- OpenCV矩阵操作