将string转为同名类名,方法名。(c#反射)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace stringConvertClass
{
class test
{
public void Method()
{
Console.WriteLine("调用成功!");
}
public void Method(testcase)
{
console.WriteLine(testcase.toString());
}
}
class Program
{
public static void run(TestCase testcase)
{
string strClass = "stringConvertClass.test"; //命名空间+类名
string strMethod = "Method";//方法名
Type t;
object obj;
t = Type.GetType(strClass);//通过string类型的strClass获得同名类“t”
System.Reflection.MethodInfo method = t.GetMethod(strMethod);//通过string类型的strMethod获得同名的方法“method”
obj = System.Activator.CreateInstance(t);//创建t类的实例 "obj"
method.Invoke(obj,null);//t类实例obj,调用方法"method"
//上面的方法是无参的,下面是有参的情况.
object[] objs = new object[]{testcase};
method.Invoke(obj,objs );//t类实例obj,调用方法"method(testcase)"
}
static void Main(string[] args)
{
TestCase testcase = new TestCase();//自己定义的类
run(testcase);
}
}
}
将string转为同名类名,方法名。(c#反射)的更多相关文章
- java反射查看jar包中所有的类名方法名
不反编译,不用其他工具,用java反射查看jar包中所有的类名方法名,网上很多都报错,下面这个你试试看:话不多说直接撸代码: import java.lang.reflect.Field; impor ...
- C# 反射总结 获取 命名空间 类名 方法名
一.获取 命名空间 类名 方法名 using System; using System.Collections.Generic; using System.Linq; using System.Tex ...
- c# 获取命名空间 类名 方法名
c# 获取命名空间 类名 方法名 转[http://blog.sina.com.cn/s/blog_3fc2dcc1010189th.html] 分类: Winform public static ...
- c# 获取方法所在的命名空间 类名 方法名
平时我们在记录日志的时候难免会需要直接记录当前方法的路径,以便查找,但是每次都输入方法名称非常的繁琐,同时如果修改了方法名称也要去手动修改日志内容,真的是劳命伤财啊,所以有了如下方法则可解决我们的大难 ...
- C#基础-获得当前程序的 空间名.类名.方法名
string typeName = this.GetType().ToString();//空间名.类名 string typeName = this.GetType().Name;//类名 new ...
- C# 获取方法所在的 命名空间 类名 方法名
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- .NET 中获取调用方法名
在写记录日志功能时,需要记录日志调用方所在的模块名.命名空间名.类名以及方法名,想到使用的是反射(涉及到反射请注意性能),但具体是哪一块儿还不了解,于是搜索,整理如下: 需要添加相应的命名空间: us ...
- [No000085]C#反射Demo,通过类名(String)创建类实例,通过方法名(String)调用方法
using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using Sy ...
- 方法名的string类型应用(补)
string strClass = "stringConvertClass.test"; //命名空间+类名 string strMethod = "Method&quo ...
随机推荐
- [easyUI] lazyload 懒加载
1.使用<img>标签将图片都写在网页上. <div style="height:450px;"><h1>请往下看,有图片的吆!</h1& ...
- 20190227xlVBA辅助输入
Dim tg As Range Dim FreeInput As Boolean Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.Retur ...
- 2018普及组摆渡车洛谷5017(dp做法)
啦啦啦,这一篇是接上一篇的博客,上一篇是记忆化搜索,而这一篇是dp+前缀和小技巧 dp这种玄学做法我这种蒟蒻当然不是自己想出来的,参考https://blog.csdn.net/kkkksc03/ar ...
- SAP跳过权限检查/前提是有debug权限
SE37,AUTH_CHECK_TCODE加断点: 执行事务代码,进入断点,修改sy-subrc = 0,执行,就可以跳过权限检查. 只给了事务代码权限,没有操作权限: CL_SUID_TOOLS== ...
- REACT map dictionary
Object.entries(obj).map(([key, value]) => ( console.log(key); console.log(value); ))
- MapReduce(五)
MapReduce的(五) 1.MapReduce的多表关联查询. 根据文本数据格式.查询多个文本中的内容关联.查询. 2.MapReduce的多任务窜执行的使用 多任务的串联执行问题,主要是要建立c ...
- less和sass的定义和区别
less是单独的一种文件,可以理解为css的升级版: sass是一个相对新的编程语言,为web前端开发而生,可以用它来定义一套新的语法规则和函数: 区别: ① 表现方式不同:less基于javas ...
- Vue(九) 自定义指令
前面介绍了许多 Vue 内置的指令,比如 v-if.v-show等,这些丰富的指令能满足我们绝大部分的业务需求,不过在需要一些特殊功能时,我们仍然希望对 DOM 进行底层的操作,这时就要用到自定义指令 ...
- Win10系列:C#应用控件基础7
Slider控件 Slider控件包含一个滑动条.一个滑动块和一个取值范围,沿滑动条移动滑动块可以在取值范围内改变Slider控件的值.Slider控件的用途很广泛,例如可以使用Slider控件来设置 ...
- linux svn 多项目设置
cd /svn/repos svnadmin create project_a svnadmin create project_b cd project_a cp -a conf /svn/ cd / ...