zoukankan      html  css  js  c++  java
  • 反射-学习笔记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);
     }
  • 相关阅读:
    HTTP协议
    idea新建工程项目结构
    idea使用的JDK版本1.9换成1.8后相关的更改设置
    Servlet
    Tomcat三种项目部署方式
    Tomcat环境变量配置命令行报错:The JRE_HOME environment variable is not defined correctl This environment variable is needed to run this program
    JDBC面试题
    XML基础入门
    数据库连接池——Druid
    $.ajax 分页
  • 原文地址:https://www.cnblogs.com/xiao9426926/p/6112764.html
Copyright © 2011-2022 走看看