将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 ...
随机推荐
- Lab 7-1
Analyze the malware found in the file Lab07-01.exe. Questions and Short Answers How does this progra ...
- eclipse get set 自动添加注释
编码的时候通常要用到 JavaBean ,而在我们经常把注释写在字段上面,但生成的Get/Set方法不会生成,通过修改Eclipse源码可解决,直接上例子: /** * 员工ID */ private ...
- MongoDB基本增删改查
一. 在Node中如何操作 MongoDB数据 1.使用官方的 mongodb 来操作:https://github.com/mongodb/node-mongodb-native 2.使用第三方 ...
- tcp滑动窗口详解(2)
http://blog.csdn.net/yujun00/article/details/636495 ARQ与滑动窗口概念 滑动窗口协议,是TCP使用的一种流量控制方法.该协议允许发送方在停止并等 ...
- mysql 安装到最后一步时,start service 为失败状态
容易出现的问题:mysql 安装到最后一步时,start service 为失败状态. 解决方法: 方式1 MySQL安装是出现could not start the service mysql ...
- java使用valueOf的方法反转字符串输出
public class FanZhuan { public static void main(String[] args) { String s = "987654321088123abo ...
- eclipse工具类及插件(修改eclipse.ini文件及作者名字)
https://jingyan.baidu.com/album/9158e0005633c0a254122807.html?picindex=1 (修改eclipse.ini文件及作者名字) http ...
- left join
left join 是以A表为基础,A表即左表,B表即右表. 左表(A)的记录会全部显示,而右表(B)只会显示符合条件表达式的记录,如果在右表(B)中没有符合条件的记录,则记录不足的地方为NULL. ...
- springBoot的数据库操作
一:操作数据库起步 1.Spring-Data-Jpa JPA定义了对象持久化的标准. 目前实现了有Hibernate,TopLink 2.pom添加依赖 <dependency> < ...
- 【leetcode】415. Add Strings
problem 415. Add Strings solution: class Solution { public: string addStrings(string num1, string nu ...