zoukankan      html  css  js  c++  java
  • 将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);
            }
        }
    }

    快乐而轻松的写代码
  • 相关阅读:
    收藏的一些前端学习的网址
    使用box-shadow 实现水波、音波的效果
    asyncjs,waterfall的使用
    兼容opacity的方法
    在php框架中写正规则表达式时的磕绊
    浏览器的渲染原理
    正规则表达式判断数字
    ie6,7,8不兼容rgba,写background时候不要写成rgba
    jquery-ias插件详解
    制作手机页面过程中遇到的一点问题
  • 原文地址:https://www.cnblogs.com/libei/p/6422941.html
Copyright © 2011-2022 走看看