using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
class test
{
public void Method()//
{
Console.WriteLine("1:__" + "Method调用成功!");
}
public void Method(string str)//
{
Console.WriteLine("2:__" + str);
} public string Method(string str1, string str2)//
{
string className1 = this.GetType().FullName;//非静态方法中获取类名
string className2 = MethodBase.GetCurrentMethod().ReflectedType.FullName;//静态方法中的获取类名
Console.WriteLine("3:__;类名"+className1+"_"+ className2);
return "3:__" + str1 + str2;
}
}
class Program
{
public static void run(string testcase)
{
string strClass = "test"; //命名空间+类名
string strMethod = "Method";//方法名 Type type;
object obj; type = Type.GetType(strClass);//通过string类型的strClass获得同名类“type”
obj = System.Activator.CreateInstance(type);//创建type类的实例 "obj" MethodInfo method = type.GetMethod(strMethod, new Type[] { });//取的方法描述//通过string类型的strMethod获得同名的方法“method”//
method.Invoke(obj, null);//type类实例obj,调用方法"method"// method = type.GetMethod(strMethod, new Type[] { typeof(String) });//取的方法描述//
object[] objs = new object[] { testcase };
method.Invoke(obj, objs);//t类实例obj,调用方法"method(testcase)"// method = type.GetMethod(strMethod, new Type[] { typeof(String), typeof(String) });//取的方法描述//
var result = (string)method.Invoke(obj, new object[] { "a", "b" });//
Console.WriteLine(result);//3 //string className = this.GetType().FullName;
string className = MethodBase.GetCurrentMethod().ReflectedType.FullName;//静态方法中的获取类名
Console.WriteLine(className);
Console.ReadKey();
}
static void Main(string[] args)
{
string testcase = "测试呀";//自己定义的类
run(testcase);
}
}

[No000085]C#反射Demo,通过类名(String)创建类实例,通过方法名(String)调用方法的更多相关文章

  1. c#输入方法名来调用方法(反射)

    using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using Sy ...

  2. c++ 模拟java的反射,根据类名动态创建类

    参考: https://blog.csdn.net/jnu_simba/article/details/9318799 原先有静态变量依赖问题, https://blog.csdn.net/anony ...

  3. C#反射实例应用--------获取程序集信息和通过类名创建类实例

    AppDomain.CurrentDomain.GetAssemblies();获取程序集,但是获取的只是已经加载的dll,引用的获取不到. System.Reflection.Assembly.Ge ...

  4. C#反射 获取程序集信息和通过类名创建类实例(转载)

    C#反射获取程序集信息和通过类名创建类实例 . System.Reflection 命名空间:包含通过检查托管代码中程序集.模块.成员.参数和其他实体的元数据来检索其相关信息的类型. Assembly ...

  5. 使用反射创建Bean、Spring中是如何根据类名配置创建Bean实例、Java提供了Class类获取类别的字段和方法,包括构造方法

    Java提供了Class类,可以通过编程方式获取类别的字段和方法,包括构造方法    获取Class类实例的方法:   类名.class   实例名.getClass()   Class.forNam ...

  6. 自己动手之使用反射和泛型,动态读取XML创建类实例并赋值

    前言: 最近小匹夫参与的游戏项目到了需要读取数据的阶段了,那么觉得自己业余时间也该实践下数据相关的内容.那么从哪入手呢?因为用的是Unity3d的游戏引擎,思来想去就选择了C#读取XML文件这个小功能 ...

  7. Swift微博项目--Swift中通过类名字符串创建类以及动态加载控制器的实现

    Swift中用类名字符串创建类(用到了命名空间) OC中可以直接通过类名的字符串转换成对应的类来操作,但是Swift中必须用到命名空间,也就是说Swift中通过字符串获取类的方式为NSClassFro ...

  8. C# 反射 通过类名创建类实例

    “反射”其实就是利用程序集的元数据信息. 反射可以有很多方法,编写程序时请先导入 System.Reflection 命名空间. 1.假设你要反射一个 DLL 中的类,并且没有引用它(即未知的类型): ...

  9. 创建类模式(一):工厂方法(Factory Method)

    定义 此模式的核心精神是封装类中不变的部分,提取其中个性化善变的部分为独立类,通过依赖注入以达到解耦.复用和方便后期维护拓展的目的. 定义一个创建产品对象的工厂接口,将实际创建工作推迟到子类当中.核心 ...

随机推荐

  1. JSTL标签功能集锦

    1.<fmt:parseNumber integerOnly="true" value="2/3" /> 结果为0 功能:fmt:parseNumb ...

  2. 000.Introduction to ASP.NET Core--【Asp.net core 介绍】

    Introduction to ASP.NET Core Asp.net core 介绍 270 of 282 people found this helpful By Daniel Roth, Ri ...

  3. jQuery组件开发之表格隔行选中效果实现

    一.效果展示如下 jQuery组件之表格插件源码 //表格选中插件 //方式一 (function($){ var chosTabBgColor = function(options){ //设置默认 ...

  4. 使用CSS3制作立体效果的导航菜单

    效果如下: 也可以点击网址查看效果:http://keleyi.com/keleyi/phtml/html5/12.htm 请使用支持CSS3的浏览器访问本页面,获得更好效果. 源代码: <st ...

  5. 在vs2012中用C#开发Android应用Xamarin环境搭建

    Xamarin是Mono创始人Miguel de Icaza创建的公司,旨在让开发者可以用C#编写iOS, Android, Mac应用程序,也就是跨平台移动开发. 简介 Xamarin是基于Mono ...

  6. 好用的第三方控件,Xcode插件(不断更新)

    第三方控件类:   1.提示框 MBProgressHUD: 是一款非常强大的.提供多种样式的提示框.使用起来简单.方便.可以在GitHub上查看具体的使用方法. https://github.com ...

  7. iOS 10 :用 UIViewPropertyAnimator 编写动画

    英文:shinobicontrols 译文:戴仓薯 链接:http://www.jianshu.com/p/4244cf130478 [iOS 10 day by day] Day 1:开发 iMes ...

  8. 基于Ruby的watir-webdriver自动化测试方案与实施(五)

    接着基于Ruby的watir-webdriver自动化测试方案与实施(四) http://www.cnblogs.com/Javame/p/4164570.html 继续 ... ... 关于特殊控件 ...

  9. 请问utf-8的中文是一个汉字占三个字节长度吗?

    这是个好问题,可以当作一个笔试题.先从字符编码讲起. 1.美国人首先对其英文字符进行了编码,也就是最早的ascii码,用一个字节的低7位来表示英文的128个字符,高1位统一为0: 2.后来欧洲人发现尼 ...

  10. [css]我要用css画幅画(六)

    接着之前的[css]我要用css画幅画(五), 由于这个画已经画了很久了,这次一次性加了比较多更新,算是让这幅画告一段落吧. 本次的更新包括: 1. 给云增加动画 2. 画了一棵树 3. 树上画了一个 ...