zoukankan      html  css  js  c++  java
  • C#反射实现类和方法的传递

    类、方法作为参数传递的写法整理。(通过反射实现)

    需要被调用的类及方法:
    public   class   Class1
    {
    public   Class1()
    {
    //
    //   TODO:   在此处添加构造函数逻辑
    //
    }
    public   int   Test1(String   s)
    {
    return   int.Parse(s)+1;
    }
    }

    调用其它类和方法的类:
    using   System.Reflection;
    public   class   ReflectMethod
    {
    public   ReflectMethod()
    {
    //
    //   TODO:   在此处添加构造函数逻辑
    //
    }
    /**参数:
      *     对象ClassName 是一个实例的对象
      *     类ClassName里面的方法MethodName名字的字符串
      *     方法MethodName的输入参数,这里设定为一个输入参数,并且为字符串
      *     方法MethodName的返回值设定为int类型
      *  
      *           本方法对类的传递是签名确定的,而类的方法的调用是对不确定的,即通过方法名的字符串来访问方法的
      *   */
    public   int   DelMethod(Object   ClassName,String   MethodName,String   Parameter_value)  
    {
    //反射
    MethodInfo   info   =   null;

    //传入类的类型
    Type     mytype=ClassName.GetType();

    //得到调用类(ClassName)的方法
    info   =mytype.GetMethod(MethodName);

    //执行方法并得到返回值
    int   mya=(int)info.Invoke(ClassName,   new   object[]   {   Parameter_value   });
    return   mya;
    }
    }

    主程序调用类Class1里面的方法的写法(通过类ReflectMethod的方法DelMethod)
    //实例化一个反射类
    ReflectMethod   myRM=new   ReflectMethod();

    //实例化一个方法类
    Class1   myc=new   Class1();

    //通过反谢调用访求
    int   t=myRM.DelMethod(myc, "Test1 ", "6023 ");

    //显示输出
    System.Diagnostics.Debug.WriteLine(t.ToString());  
  • 相关阅读:
    mysql之存储过程
    Artificial Intelligence in Finance
    7 Exciting Uses of Machine Learning in FinTech
    Machine Learning in Finance – Present and Future Applications
    AI AND THE BOTTOM LINE: 15 EXAMPLES OF ARTIFICIAL INTELLIGENCE IN FINANCE
    5 Ways AI is Transforming the Finance Industry
    图学Kubernetes
    Free Professional Resume Examples and Writing Tips
    npm和yarn的淘宝镜像添加
    世界最具影响力的十大管理大师
  • 原文地址:https://www.cnblogs.com/leeolevis/p/1383149.html
Copyright © 2011-2022 走看看