将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 ...
随机推荐
- LeetCode--015--三元之和(java)
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...
- 深入理解Plasma(二)Plasma 细节
这一系列文章将围绕以太坊的二层扩容框架,介绍其基本运行原理,具体操作细节,安全性讨论以及未来研究方向等.本篇文章主要对 Plasma 一些关键操作的细节进行剖析. 在上一篇文章中我们已经理解了什么是 ...
- Linux下执行.sh命令出现-bash: ./bin/start.sh: /bin/bash^M: bad interpreter: No such file or directory
原因是 文件的格式是dos,修改为unix 就OK了 查看文件格式 用vim 打开出错的文件 按 ESC键 再按shift+冒号 输入 set ff 回车 可以看见 该文件 ...
- ml交叉验证
https://blog.csdn.net/guanyuqiu/article/details/86006474 https://blog.csdn.net/weixin_42660173/artic ...
- dedecms 模版里格式化时间标签
<!--带时分秒--> [field:pubdate function="GetDateTimeMK(@me)"/] <!--只有日期--> [field: ...
- Canvas---clearRect()清除圆形区域
function clearArcFun(x,y,r,cxt){ //(x,y)为要清除的圆的圆心,r为半径,cxt为context var stepClear=1;//别忘记这一步 clearArc ...
- linux安装nginx,遇坑解决
1.nginx官网下载tar包,解压linux下: 2.进入解压文件夹,执行./configure: 3.报错,原因没有安装nginx相关依赖,如gcc环境,PCRE依赖库 ,zlib 依赖库 ,Op ...
- Java--- Ajax异步验证用户名是否存在
今天复习了一下 Ajax异步验证用户名是否存在,在下面我写三种,一般开发时候都用第三种 jQuery和Ajax结合的形式,下面就介绍三种,具体说明代码里就有啦,废话不多说,直接上代码: 第一种方式: ...
- [Leetcode 105]*前序后序遍历形成树
public TreeNode find(int[] preorder, int[] inorder,int j, int start, int end) { if (j > preorder. ...
- 自动化测试badboy脚本开发(一)
badboy的检查点: 检查点设置例子:以上一节脚本录制方法简单录制搜索“badboy”脚本,在搜索框中选中搜索内容(注意录制脚本后要停止录制)→点击工具栏中的Tools→选择Add Assertio ...