将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 ...
随机推荐
- C#执行EXE程序
第一种方法:System.Diagnostics.ProcessStartInfo info =new System.Diagnostics.ProcessStartInfo(path);info.W ...
- linux文件管理之链接文件
文件链接 ====================================================================================软链接 或 符号链接硬 ...
- Introducing the Microservices Reference Architecture from NGINX
Introducing the Microservices Reference Architecture from NGINX https://www.nginx.com/blog/introduci ...
- HTTP安全通信:Https和SSL
1. HTTPS概念 1)简介 HTTPS(全称:Hypertext Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HT ...
- simple shell
NOW = $(data + "%Y-%m-%d-%H-%M-%S") DIR = /export/backup function exportData(){ exportCmd ...
- 『Python CoolBook』C扩展库_其二_demo演示
点击进入项目 C函数源文件 /* sample.c */ #include "sample.h" /* Compute the greatest common divisor */ ...
- Windows Server 2012 R2域控制器部署
1. 概述 该文档描述了在Windows 2012R2 系统上搭建域控的方式. 2. 具体步骤 2.1 首先我们先配置好IP地址.计算名(默认的计算机名比较长,后期其它计算机加入域控的时候需要输入比较 ...
- 1)python,现在用过的,后面推出每日练习语法与深度挖掘应用
python 1, Mac 系统自带的python路径 /System/Library/Frameworks/Python.framework/Version里面存放多个版本 可通过:启动python ...
- JS碰撞检测
视图理解://div1的上边大于div2的下边,,div1的右边小于div2的左边,,div1的上边大于div2的下边,,div1的左边大于div2的右边,这四种情况,问题是没有碰撞/重叠,如下: & ...
- javascript 位操作符
not: 按位非,符号为波浪线~ 作用吧二进制数的所有位进行非操作,对应的十进制结果为原先10进制数字取负值然后减去1 其他的操作符感觉不是很常用,分别为按位与(&),按位或(|),左移(&l ...