将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 ...
随机推荐
- 获取url "?" 后面的字符串
今天写了一个URL “?” 后面的字符串 来改变当前页面的状态 首先需要获取当前页面的URL console.log(widow.location) 之后页面就会打印出来当前的URL 之后我们获取UR ...
- jvm回收器回收过程一:CMS和 G1的初认知(持续更新中)
CMS:介绍: 1.CMS(Concurrent Mark-Sweep)是以牺牲吞吐量为代价来获得最短回收停顿时间的垃圾回收器.对于要求服务器响应速度的应用上,这种垃圾回收器非常适合. 在启动JVM参 ...
- Ubuntu 16.04+GTX970 黑屏无法安装解决方法
参考http://www.linuxidc.com/Linux/2017-01/139318.htm http://blog.sciencenet.cn/blog-655584-877622.html ...
- js如何在浏览器添加cookie(添加、获取、删除)
首先,简单用js将cookie保存到浏览器中,具体可按F12在这里查看(火狐浏览器) 1.js脚本方法 1)添加cookie方法 //添加cookie var addCookie = function ...
- 浅谈对象的两个方法:Object.keys() ,Object.assign();
1 : Object.keys(obj) 返回给定对象的所有可枚举属性的字符串数组 例子1: var arr = [1, 2, 6, 20, 1]; console.log(Object.keys(a ...
- Win10系列:C#应用控件基础23
Telerik UI Controls for Windows 8 Telerik UI Controls for Windows 8是一套为创建Windows UWP应用而设计的工具集,开发人员可以 ...
- 跟随我在oracle学习php(3)
这次讲一下html中的列表和比较重要的表格 列表分为有序和无序,有序列表与无序列表都是块状元素 <ul>(父标签) 定义无序列表.复合标签(由父标签和子标签组成),不单独出现,用<l ...
- Nearest Common Ancestors (POJ 1330)
A rooted tree is a well-known data structure in computer science and engineering. An example is show ...
- 【阅读笔记】《C程序员 从校园到职场》第七章 指针和结构体
原文地址:让你提前认识软件开发(13):指针及结构体的使用 CSDN博客 https://blog.csdn.net/zhouzhaoxiong1227/article/details/2387299 ...
- SpringApplication执行流程
SpringApplication类的直接作用是在main方法中通过自有的run方法启动spring应用. 具体的run方法为: public static ConfigurableApplicati ...