zoukankan      html  css  js  c++  java
  • c# 反射加读取类、方法、特性、破坏单例

     /// 1 dll-IL-metadata-反射
     /// 2 反射加载dll,读取module、类、方法、特性
     /// 3 破坏单例 创建泛型
    namespace MyReflection
    {
        class Program
        {
           
            static void Main(string[] args)
            {
                #region Reflection
                {
                    Assembly assembly = Assembly.Load("DB.Mysql");
                    //1 动态加载 一个完整dll名称不需要后缀  从exe所在的路径进行查找
                    Assembly assembly1 = Assembly.LoadFile(@"E:MyReflectionMyReflectioninDebugDB.Mysql.dll");//完整路径
                    Assembly assembly2 = Assembly.LoadFrom("DB.Mysql.dll");//当前路径
                    Assembly assembly3 = Assembly.LoadFrom(@"E:MyReflectionMyReflectioninDebugDB.Mysql.dll");//当前路径
    
                    foreach (var type in assembly.GetTypes())
                    {
                        //type.IsGenericType
                        Console.WriteLine(type.Name);       //类名
                        foreach (var method in type.GetMethods())
                        {
                            Console.WriteLine(method.Name);//方法名
                        }
                    }
                }
                #endregion
    
                #region MyRegion
                {
                    Assembly assembly = Assembly.Load("DB.Mysql");         //1 动态加载
                    Type type = assembly.GetType("DB.Mysql.MySqlHelper");//2 获取类型 完整类型名称
                    object oDBHelper = Activator.CreateInstance(type);//3 创建对象
    
                    //oDBHelper.Query();
                    //不能直接Query  ,实际上oDBHelper是有Query方法的,只是因为编译器不认可
                    //C#是一种强类型语言,静态语言,编译时就确定好类型保证安全
                    //dynamic dDBHelper= Activator.CreateInstance(type);
                    //dDBHelper.Query();//dynamic编译器不检查,,运行时才检查
                    IDBHelper iDBHelper = oDBHelper as IDBHelper;//4 类型转换  不报错,类型不对就返回null
                    iDBHelper.Query();//5 方法调用
                }
                #endregion
                {
                    Console.WriteLine("**************ctor&parameter*************");
                    Assembly assembly = Assembly.Load("DB.Mysql");
                    Type type = assembly.GetType("DB.Mysql.MySqlHelper");
                    foreach (ConstructorInfo ctor in type.GetConstructors())
                    {
                        Console.WriteLine(ctor.Name);
                        foreach (var parameter in ctor.GetParameters())
                        {
                            Console.WriteLine(parameter.ParameterType);   //构造函数参数类型
                        }
                    }
                    object oTest1 = Activator.CreateInstance(type);
                    object oTest2 = Activator.CreateInstance(type, new object[] { 123 });  //调用有参数构造函数
                    object oTest3 = Activator.CreateInstance(type, new object[] { "陌殇" });
                }
                {
                    //反射破坏单例---就是发射调用私有构造函数
                    Assembly assembly = Assembly.Load("DB.Mysql");
                    Type type = assembly.GetType("DB.Mysql.Singleton");
                    Singleton singletonA = (Singleton)Activator.CreateInstance(type, true);
                    Singleton singletonB = (Singleton)Activator.CreateInstance(type, true);
                    Console.WriteLine($"{object.ReferenceEquals(singletonA, singletonB)}");
                }
                #region 反射创建泛型
                {
                    Assembly assembly = Assembly.Load("DB.Mysql");
                    Type type = assembly.GetType("DB.Mysql.Generic`3");
                    Type typeMake = type.MakeGenericType(new Type[] { typeof(string), typeof(int), typeof(DateTime) });
                    object oGeneric = Activator.CreateInstance(typeMake);
                }
                #endregion
                Console.ReadKey();
            }
        }
    }
    namespace DB.Interface
    {
        public interface IDBHelper
        {
            void Query();
        }
    }
    namespace DB.Mysql
    {
        public class MySqlHelper : IDBHelper
        {
            public string name { get; set; }
            public MySqlHelper()
            {
                Console.WriteLine("{0}被构造", this.GetType().Name);
            }
            /// <summary>
            /// 带参数构造函数
            /// </summary>
            /// <param name="name"></param>
            public MySqlHelper(string name)
            {
                Console.WriteLine("这里是{0} 有参数构造函数", this.GetType());
            }
    
            public MySqlHelper(int id)
            {
                Console.WriteLine("这里是{0} 有参数构造函数", this.GetType());
            }
    
            public void Test()
            {
                Console.WriteLine("");
            }
    
            public void Query()
            {
                Console.WriteLine("{0}.Query", this.GetType().Name);
            }
        }
        public class SqlServerHelper
        {
            public SqlServerHelper()
            {
                Console.WriteLine("{0}被构造", this.GetType().Name);
            }
        }
    }
    namespace DB.Mysql
    {
        public sealed class Singleton
        {
            private static Singleton _Singleton = null;
            private Singleton()
            {
                Console.WriteLine("Singleton被构造");
            }
    
            static Singleton()
            {
                _Singleton = new Singleton();
            }
    
            public static Singleton GetInstance()
            {
                return _Singleton;
            }
        }
    }
    namespace DB.Mysql
    {
        public class MySqlHelper : IDBHelper
        {
            public string name { get; set; }
            public MySqlHelper()
            {
                Console.WriteLine("{0}被构造", this.GetType().Name);
            }
            /// <summary>
            /// 带参数构造函数
            /// </summary>
            /// <param name="name"></param>
            public MySqlHelper(string name)
            {
                Console.WriteLine("这里是{0} 有参数构造函数", this.GetType());
            }
    
            public MySqlHelper(int id)
            {
                Console.WriteLine("这里是{0} 有参数构造函数", this.GetType());
            }
    
            public void Test()
            {
                Console.WriteLine("");
            }
    
            public void Query()
            {
                Console.WriteLine("{0}.Query", this.GetType().Name);
            }
        }
        public class SqlServerHelper
        {
            public SqlServerHelper()
            {
                Console.WriteLine("{0}被构造", this.GetType().Name);
            }
        }
    }
  • 相关阅读:
    Alice and Bob 要用到辗转相减
    Java经典设计模式
    设计模式---创建类---建造者模式
    luogu4267 TamingtheHerd (dp)
    nowcoder172C 保护 (倍增lca+dfs序+主席树)
    nowcoder172A 中位数 (二分答案)
    bzoj4985 评分 (二分答案+dp)
    luogu4269 Snow Boots G (并查集)
    luogu4268 Directory Traversal (dfs)
    bzoj1001/luogu4001 狼抓兔子 (最小割/平面图最小割转对偶图最短路)
  • 原文地址:https://www.cnblogs.com/wangdash/p/11865341.html
Copyright © 2011-2022 走看看