反射——反射的属性和方法

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:\lv\C#练习\01Reflection\lab1\bin\Debug\lab1.dll";
            //Assembly asm = Assembly.LoadFile(filePath);
            #endregion
            string filePath = @"C:\lv\C#练习\01Reflection\lab1\bin\Debug\lab1.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方法

 

转载于:https://www.cnblogs.com/lv-sally/p/4727377.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值