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:\MyReflection\MyReflection\bin\Debug\DB.Mysql.dll");//完整路径 Assembly assembly2 = Assembly.LoadFrom("DB.Mysql.dll");//当前路径 Assembly assembly3 = Assembly.LoadFrom(@"E:\MyReflection\MyReflection\bin\Debug\DB.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¶meter*************"); 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); } } }