zoukankan      html  css  js  c++  java
  • 反射——反射的属性和方法

    1 创建实例

    System.Reflection.Assembly ass = Assembly.LoadFrom(Server.MapPath("bin/swordyang.dll")); //加载DLL
                System.Type t = ass.GetType("cn.SwordYang.TextClass");//获得类型
                object o = System.Activator.CreateInstance(t);//创建实例
    
                System.Reflection.MethodInfo mi = t.GetMethod("RunJs");//获得方法
    
    
                mi.Invoke(o, new object[] { this.Page,"alert('测试反射机制')"});//调用方法
    View Code

    2 获取其他

    namespace _03Reflection
    {
        class Program
        {
            static void Main(string[] args)
            {
                person p = new person();
                #region  使用type类的三种方法
                ////第一种
                //Type t = p.GetType();
                ////第二种
                //Type t2 = typeof(person);
                ////第三种
                //string filePath = @"C:lvC#练习1Reflectionlab1inDebuglab1.dll";
                //Assembly asm = Assembly.LoadFile(filePath);
                #endregion
                string filePath = @"C:lvC#练习1Reflectionlab1inDebuglab1.dll";
                Assembly asm = Assembly.LoadFile(filePath);
                //person类型
                Type typePerson = asm.GetType("lab1.person");
                //person子类型
                Type typeChild1 = asm.GetType("lab1.person2");
    
                Type typeChild = asm.GetType("lab1.person4");
                //委托类型
                Type typeDelegate = asm.GetType("lab1.Flayable");
                //接口类型
                Type typeInterface = asm.GetType("lab1.IFlayable");
                //抽象类型
                Type typeAbs = asm.GetType("lab1.Animal");
    
                //判断person4是否为person的子类
                bool b = typePerson.IsAssignableFrom(typeChild);
                bool b2 = typePerson.IsAssignableFrom(typeChild1);
                bool b3 = typeChild1.IsSubclassOf(typePerson);
    
                Console.WriteLine(b);
                Console.WriteLine(b2);
                Console.WriteLine(b3);
                
                Console.WriteLine(typeDelegate.IsAbstract);//false
                //接口,抽象类,静态了都不能实例化,认为是抽象的
                Console.WriteLine(typeInterface.IsAbstract);
                Console.WriteLine(typeAbs.IsAbstract);
                Console.ReadKey();
            }
        }
        public class person
        {
            private int age;
            public string Name
            {
                get;
                set;
            }
    
            public int Age
            {
                set { this.age = value; }
                get { return this.age; }
            }
    
            public void SayHi()
            {
                Console.WriteLine("hello");
            }
        }
    }
    View Code

    1 Activator.CreateInstance(Type t)会动态调用类的无参的构造函数创建一个对象,返回值是创建的对象,如果类没有无参的构造函数就会报错

    2 GetConstructor(参数列表);这个是找到带参数的构造函数

    3 type类的方法

        type是实现反射的一个重要的类,通过它我们可以获取类中的所有信息。包括方法和属性

        在编写调用插件的时候会做一系列的验证

    bool IsAssignableFrom(type c)判断当前类型的变量可不可以接受c类型变量的赋值     

    bool IsInstanceOfType(object o)判断当前对象o是否是当前类的实例(当前类可以是o的类、父类、子类)

    bool IssubclassOf(type c)判断当前类类c的子类。与接口无关

    IsAbsstract,判断是否是抽象的,包含抽象类,接口和静态类

    GetExportedTypes()只获取public类型

    GetTypes()获取所有类型

    xxx.GetType("TestLib.Class1").GetMethods();获取class中所有的方法

    xxx.GetType("TestLib.Class1").GetMethod(“sayHi”);获取sayHi方法

  • 相关阅读:
    Insertion Sort List
    Max Points on a Line
    Evaluate Reverse Polish Notation
    vue路由传参的三种基本方式
    如何搭建一个vue项目
    vue路由跳转时更改页面title
    CSS清除浮动大全共8种方法
    border:none 与border:0的区别
    for..in和for..of的功能
    IE浏览器兼容问题-----html和css的兼容写法
  • 原文地址:https://www.cnblogs.com/lv-sally/p/4727377.html
Copyright © 2011-2022 走看看