using System.Reflection;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 Assembly assembly = Assembly.Load("Ruanmou.DB.MySql");//获取当前路径下的dll,不要后缀 2 Assembly assembly1 = Assembly.LoadFile(@"D:\ruanmou\online9\20170607Advanced9Course2Reflection\MyReflection\MyReflection\bin\Debug\Ruanmou.DB.MySql.dll"); 3 Assembly assembly2 = Assembly.LoadFrom("Ruanmou.DB.MySql.dll");
1 foreach (var item in assembly.GetModules()) 2 { 3 Console.WriteLine(item.Name); 4 } 5 foreach (var item in assembly.GetTypes())
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 Console.WriteLine("************************反射调用实例方法、静态方法、重载方法*****************"); 2 Assembly assembly = Assembly.Load("Ruanmou.DB.SqlServer"); 3 Type type = assembly.GetType("Ruanmou.DB.SqlServer.ReflectionTest"); 4 object oTest = Activator.CreateInstance(type); 5 foreach (var item in type.GetMethods()) 6 { 7 Console.WriteLine(item.Name); 8 } 9 { 10 MethodInfo method = type.GetMethod("Show1"); 11 method.Invoke(oTest, null); 12 } 13 { 14 MethodInfo method = type.GetMethod("Show5"); 15 method.Invoke(oTest, new object[] { "电脑信息技术" }); 16 method.Invoke(null, new object[] { "装逼的岁月" }); 17 } 18 { 19 MethodInfo method = type.GetMethod("Show3", new Type[] { typeof(int) }); 20 method.Invoke(oTest, new object[] { 123 }); 21 } 22 { 23 MethodInfo method = type.GetMethod("Show3", new Type[] { typeof(string) }); 24 method.Invoke(oTest, new object[] { "吝啬小兔" }); 25 } 26 { 27 MethodInfo method = type.GetMethod("Show3", new Type[] { typeof(int), typeof(string) }); 28 method.Invoke(oTest, new object[] { 123, "浅步调" }); 29 } 30 { 31 MethodInfo method = type.GetMethod("Show3", new Type[] { typeof(string), typeof(int) }); 32 method.Invoke(oTest, new object[] { "心里要有阳光", 123 }); 33 } 34 } 35 #endregion
6 { 7 Console.WriteLine(item.Name); 8 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #region Method 2 /// <summary> 3 /// 无参方法 4 /// </summary> 5 public void Show1() 6 { 7 Console.WriteLine("这里是{0}的Show1", this.GetType()); 8 } 9 /// <summary> 10 /// 有参数方法 11 /// </summary> 12 /// <param name="id"></param> 13 public void Show2(int id) 14 { 15 16 Console.WriteLine("这里是{0}的Show2", this.GetType()); 17 } 18 /// <summary> 19 /// 重载方法之一 20 /// </summary> 21 /// <param name="id"></param> 22 /// <param name="name"></param> 23 public void Show3(int id, string name) 24 { 25 Console.WriteLine("这里是{0}的Show3", this.GetType()); 26 } 27 /// <summary> 28 /// 重载方法之二 29 /// </summary> 30 /// <param name="name"></param> 31 /// <param name="id"></param> 32 public void Show3(string name, int id) 33 { 34 Console.WriteLine("这里是{0}的Show3_2", this.GetType()); 35 } 36 /// <summary> 37 /// 重载方法之三 38 /// </summary> 39 /// <param name="id"></param> 40 public void Show3(int id) 41 { 42 43 Console.WriteLine("这里是{0}的Show3_3", this.GetType()); 44 } 45 /// <summary> 46 /// 重载方法之四 47 /// </summary> 48 /// <param name="name"></param> 49 public void Show3(string name) 50 { 51 52 Console.WriteLine("这里是{0}的Show3_4", this.GetType()); 53 } 54 /// <summary> 55 /// 重载方法之五 56 /// </summary> 57 public void Show3() 58 { 59 60 Console.WriteLine("这里是{0}的Show3_1", this.GetType()); 61 } 62 /// <summary> 63 /// 私有方法 64 /// </summary> 65 /// <param name="name"></param> 66 private void Show4(string name) 67 { 68 Console.WriteLine("这里是{0}的Show4", this.GetType()); 69 } 70 /// <summary> 71 /// 静态方法 72 /// </summary> 73 /// <param name="name"></param> 74 public static void Show5(string name) 75 { 76 Console.WriteLine("这里是{0}的Show5", typeof(ReflectionTest)); 77 } 78 #endregion
反射破坏单例
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 namespace Ruanmou.DB.SqlServer 2 { 3 /// <summary> 4 /// 单例模式 5 /// </summary> 6 public sealed class Singleton 7 { 8 private static Singleton _Singleton = null; 9 private Singleton() 10 { 11 Console.WriteLine("Singleton被构造"); 12 } 13 14 static Singleton() 15 { 16 _Singleton = new Singleton(); 17 } 18 19 public static Singleton GetInstance() 20 { 21 return _Singleton; 22 } 23 } 24 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #region 多构造函数 破坏单例 创建泛型 2 { 3 Console.WriteLine("************************多构造函数 破坏单例 创建泛型 *****************"); 4 Assembly assembly = Assembly.Load("Ruanmou.DB.SqlServer"); 5 Type type = assembly.GetType("Ruanmou.DB.SqlServer.ReflectionTest"); 6 foreach (var item in type.GetConstructors()) 7 { 8 Console.WriteLine(item.Name); 9 } 10 object oTest = Activator.CreateInstance(type); 11 object oTest1 = Activator.CreateInstance(type, new object[] { 784 }); 12 object oTest2 = Activator.CreateInstance(type, new object[] { "lz" }); 13 14 Type singletonType = assembly.GetType("Ruanmou.DB.SqlServer.Singleton"); 15 Singleton singleton = Singleton.GetInstance();// new Singleton(); 16 object oSingleton1 = Activator.CreateInstance(singletonType, true); 17 object oSingleton2 = Activator.CreateInstance(singletonType, true); 18 object oSingleton3 = Activator.CreateInstance(singletonType, true); 19 object oSingleton4 = Activator.CreateInstance(singletonType, true); 20 21 Type genericType = assembly.GetType("Ruanmou.DB.SqlServer.GenericClass`3"); 22 Type typeNew = genericType.MakeGenericType(typeof(int), typeof(int), typeof(int)); 23 object oGeneric = Activator.CreateInstance(typeNew); 24 } 25 #endregion
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class GenericClass<T, W, X> 2 { 3 public void Show(T t, W w, X x) 4 { 5 Console.WriteLine("t.type={0},w.type={1},x.type={2}", t.GetType().Name, w.GetType().Name, x.GetType().Name); 6 } 7 } 8 9 public class GenericMethod 10 { 11 public void Show<T, W, X>(T t, W w, X x) 12 { 13 Console.WriteLine("t.type={0},w.type={1},x.type={2}", t.GetType().Name, w.GetType().Name, x.GetType().Name); 14 } 15 } 16 17 public class GenericDouble<T> 18 { 19 public void Show<W, X>(T t, W w, X x) 20 { 21 Console.WriteLine("t.type={0},w.type={1},x.type={2}", t.GetType().Name, w.GetType().Name, x.GetType().Name); 22 } 23 }
反射调用私有方法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #region 调用私有方法 调用泛型方法 2 { 3 Console.WriteLine("************************调用私有方法 调用泛型方法*****************"); 4 Assembly assembly = Assembly.Load("Ruanmou.DB.SqlServer"); 5 Type type = assembly.GetType("Ruanmou.DB.SqlServer.ReflectionTest"); 6 object oTest = Activator.CreateInstance(type); 7 8 { 9 MethodInfo method = type.GetMethod("Show4", BindingFlags.Instance | BindingFlags.NonPublic); 10 method.Invoke(oTest, new object[] { "幸福你我" }); 11 } 12 13 Type genericType = assembly.GetType("Ruanmou.DB.SqlServer.GenericMethod"); 14 object oGeneric = Activator.CreateInstance(genericType); 15 { 16 MethodInfo method = genericType.GetMethod("Show"); 17 MethodInfo methodNew = method.MakeGenericMethod(typeof(int), typeof(int), typeof(int)); 18 methodNew.Invoke(oGeneric, new object[] { 1, 2, 3 }); 19 } 20 } 21 #endregion
反射赋值与属性和字段
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #region 字段和属性 2 { 3 Console.WriteLine("************************Common*****************"); 4 People people = new People(); 5 people.Id = 506; 6 people.Name = "yoyo"; 7 people.Description = "勤学的聪明妹子"; 8 Console.WriteLine("Id={0}", people.Id); 9 Console.WriteLine("Name={0}", people.Name); 10 Console.WriteLine("Description={0}", people.Description); 11 12 Console.WriteLine("************************字段和属性*****************"); 13 Type type = typeof(People); 14 object oPeople = Activator.CreateInstance(type); 15 foreach (var item in type.GetProperties()) 16 { 17 Console.WriteLine("{0}={1}", item.Name, item.GetValue(oPeople)); 18 if (item.Name.Equals("Id")) 19 { 20 item.SetValue(oPeople, 787); 21 } 22 else if (item.Name.Equals("Name")) 23 { 24 item.SetValue(oPeople, "我行我素"); 25 } 26 Console.WriteLine("{0}={1}", item.Name, item.GetValue(oPeople)); 27 } 28 foreach (var item in type.GetFields()) 29 { 30 Console.WriteLine("{0}={1}", item.Name, item.GetValue(oPeople)); 31 item.SetValue(oPeople, "聪明的小伙子"); 32 Console.WriteLine("{0}={1}", item.Name, item.GetValue(oPeople)); 33 } 34 } 35 36 #endregion
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 namespace Ruanmou.Model 2 { 3 /// <summary> 4 /// 实体 5 /// </summary> 6 public class People 7 { 8 public People() 9 { 10 Console.WriteLine("{0}被创建", this.GetType().FullName); 11 } 12 13 public int Id { get; set; } 14 public string Name { get; set; } 15 16 public string Description; 17 } 18 }
拓展:反射与ado.net
1 #region sql 2 { 3 SqlServerHelper helper = new SqlServerHelper(); 4 Company company = helper.Get<Company>(1); 5 } 6 #endregion
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 namespace Ruanmou.DB.SqlServer 2 { 3 /// <summary> 4 /// SqlServer实现 5 /// </summary> 6 public class SqlServerHelper : IDBHelper 7 { 8 private static string ConnectionStringCustomers = ConfigurationManager.ConnectionStrings["Customers"].ConnectionString; 9 10 public SqlServerHelper() 11 { 12 //Console.WriteLine("{0}被构造", this.GetType().Name); 13 } 14 15 public void Query() 16 { 17 //Console.WriteLine("{0}.Query", this.GetType().Name); 18 } 19 20 /// <summary> 21 /// 一个方法满足不同的数据实体查询 22 /// </summary> 23 /// <typeparam name="T"></typeparam> 24 /// <returns></returns> 25 public T Get<T>(int id) 26 { 27 Type type = typeof(T); 28 string columnStrings = string.Join(",", type.GetProperties().Select(p => string.Format("[{0}]", p.Name))); 29 30 string sql = string.Format("SELECT {0} FROM [{1}] Where Id={2}" 31 , columnStrings 32 , type.Name 33 , id); 34 35 object t = Activator.CreateInstance(type); 36 using (SqlConnection conn = new SqlConnection(ConnectionStringCustomers)) 37 { 38 SqlCommand command = new SqlCommand(sql, conn); 39 conn.Open(); 40 SqlDataReader reader = command.ExecuteReader(); 41 if (reader.Read()) 42 { 43 foreach (var item in type.GetProperties()) 44 { 45 item.SetValue(t, reader[item.Name]); 46 } 47 } 48 } 49 50 return (T)t; 51 52 //return default(T); 53 } 54 } 55 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <startup> 4 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 5 </startup> 6 <connectionStrings> 7 <!--数据库连接字符串--> 8 <add name="Customers" connectionString="Data Source=ElevenPC; Database=Customers; User ID=sa; Password=Passw0rd; MultipleActiveResultSets=True" providerName="System.Data.SqlClient" /> 9 </connectionStrings> 10 <appSettings> 11 <add key="IDBHelperConfig" value="Ruanmou.DB.MySql.MySqlHelper,Ruanmou.DB.MySql"/> 12 13 </appSettings> 14 </configuration>