反射-学习笔记2

反射调用与单例破坏

1. 可配置可扩展
2. 去掉接口,反射调用方法,突破访问限制
3. 反射破坏单例

 

反射直接调用方法

  {
     Console.WriteLine("*********************reflection method************************");
     Assembly assembly = Assembly.Load("DB.SqlServer");// 1. 动态加载dll,参数为dll的名称,没有后缀
     Type type = assembly.GetType("DB.SqlServer.SqlSeverHelper");//2.获取类型信息,传递完整类型名称,namespace+classname
     object oObject = Activator.CreateInstance(type);//3.创建对象
     MethodInfo method = type.GetMethod("Query");//4.调用方法;在这里已经不需要接口了
     method.Invoke(oObject, null);// 第一个参数需要是个实例
  }

 

当调用的方法是静态方法,重载方法

 {
     Console.WriteLine("*********************Method Show************************");
     Assembly assembly = Assembly.Load("DB.SqlServer");// 1. 动态加载dll,参数为dll的名称,没有后缀
     Type type = assembly.GetType("DB.SqlServer.ReflectionTest");//2.获取类型信息,传递完整类型名称,namespace+classname
     object oObject = Activator.CreateInstance(type);//3.创建对象
     {
        MethodInfo method=type .GetMethod ("Show5"); //静态方法
        method.Invoke(null, new object[] { "YoYo" }); //不需要实例,第一个参数为null
     }
     {
        MethodInfo method = type.GetMethod("Show3", new Type[] { }); //重载方法
        method.Invoke(oObject, null); //第一个参数需要指定实例
     }
     {
        MethodInfo method = type.GetMethod("Show3",new Type[] { typeof(int)}); //重载方法,后面的参数要指定传递的参数类型
        method.Invoke(oObject, new object[] { 506 }); //不需要实例,第一个参数为null
     }
     {
        MethodInfo method = type.GetMethod("Show3", new Type[] { typeof(string) }); //重载方法
        method.Invoke(oObject, new object[] { "YoYo"}); //不需要实例,第一个参数为null
     }
     {
        MethodInfo method = type.GetMethod("Show3", new Type[] { typeof(string),typeof(int) }); //重载方法
        method.Invoke(oObject, new object[] { "YoYo" ,506}); //不需要实例,第一个参数为null
     }
 }

 

        //重载
        public void Show3()
        {
            Console.WriteLine("这里是{0}的Show3", this.GetType());
        }
        public void Show3(int id)
        {
            Console.WriteLine("这里是{0}的Show3_1", this.GetType());
        }
        public void Show3(string name)
        {
            Console.WriteLine("这里是{0}的Show3_2", this.GetType());
        }
        public void Show3(int id,string name)
        {
            Console.WriteLine("这里是{0}的Show3_3", this.GetType());
        }
        public void Show3(string name,int id)
        {
            Console.WriteLine("这里是{0}的Show3_4", this.GetType());
        }

        //静态方法
        public static void Show5(string name)
        {
            Console.WriteLine("这里是{0}的Show5", typeof(ReflectionTest ));
        }

 

反射破坏单例

        //私有方法
        private void Show4(string name) 
        {
            Console.WriteLine("这里是{0}的Show4", this.GetType());
        }
    public sealed class Singleton
    {
        private Singleton ()
        {
            Console.WriteLine("初始化一次");
        }
        private static Singleton instance = new Singleton();
        public static Singleton CreateInstance()
        {
            return instance;
        }
    }
 {
          Console.WriteLine("*********************单例************************");
          Singleton sinleton1 = Singleton.CreateInstance();//单例模式 多次创建 只有一个实例
          Singleton sinleton2 = Singleton.CreateInstance();
          Singleton sinleton3 = Singleton.CreateInstance();
          Singleton sinleton4 = Singleton.CreateInstance();

          Console.WriteLine("*********************反射破坏单例************************");
          Assembly assembly = Assembly.Load("DB.SqlServer");// 1. 动态加载dll,参数为dll的名称,没有后缀
          Type type = assembly.GetType("DB.SqlServer.Singleton");//2.获取类型信息,传递完整类型名称,namespace+classname
          object oObject1 = Activator.CreateInstance(type,true);//反射破坏private限制,每次都会重新创建
          object oObject2 = Activator.CreateInstance(type, true);
          object oObject3 = Activator.CreateInstance(type, true);
          object oObject4 = Activator.CreateInstance(type, true);
 }

 

转载于:https://www.cnblogs.com/xiao9426926/p/6112764.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值