zoukankan      html  css  js  c++  java
  • .net Reflection(反射)- 二

    反射 Reflection 中访问方法 

    新建一个ClassLibrary类库:

        public class Student
        {
            public string Name
            { get; set; }
            public string School
            { get; set; }
    
            public int Sum(int a, int b)
            {
                return a + b;
            }
            public string GetName()
            {
                return "this is book" ;
            }   
        }
        /// <summary>
        /// 反射
        /// </summary>
        class Program
        {
    
    
            static void Main(string[] args)
            {
                
                Assembly assembly = Assembly.Load("ClassLibrary");
    //调用 程序集 类的方法
                //创建该对象的实例,object类型,参数(名称空间+类)   
                object instances = assembly.CreateInstance("ClassLibrary.Student");
    
                //无参数 返回
                object value = type.GetMethod("GetName").Invoke(instances, null);
           //返回this is book
                //有参
                object[] params_obj = new object[2];
                params_obj[0] = 12; 
                params_obj[1] = 23;
                object value1 = type.GetMethod("Sum").Invoke(instances, params_obj);
       //返回a b和 }

    还可以通过 FindInterfaces 方法返回 接口信息 

      //获取程序集接口 Stu 继承的所有接口
                Type t = typeof(Stu);
                Type[] interfaces = t.FindInterfaces(TypeFilter, assembly);
    
                //显示每个接口信息
                foreach (Type i in interfaces)
                {
                    //获取映射接口方法的类型的方法
                    InterfaceMapping mapping = t.GetInterfaceMap(i);
    
                    for (int j = 0; j < mapping.InterfaceMethods.Length; j++)
                    {
                        Console.WriteLine(" {0} 实现的 {1}", mapping.InterfaceMethods[j], mapping.TargetMethods[j]);
                    }
                }
                 
    

      

  • 相关阅读:
    有限元学习
    软件推荐-c#绘图插件echart
    驾驶证到期换证
    实战fortran77基础语法2
    c语言spline
    软件推荐-有道超级计算器
    师弟推荐软件-/mathpix
    张奎师弟参与devexpress chartControl绘图--解决了devexpress的chartControl控件不能添加系列的问题
    Struts2之Json插件的使用
    Struts2之防止表单重复提交
  • 原文地址:https://www.cnblogs.com/dragon-L/p/3716302.html
Copyright © 2011-2022 走看看