反射(操作MetaData)
生活随笔
收集整理的這篇文章主要介紹了
反射(操作MetaData)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.泛型類反射(類型泛型數和方法泛型數一致)
public static void ShowGeneric(){Assembly a1 = Assembly.Load("ClassDemo1");//DLLType type = a1.GetType("ClassDemo1.GenericTest`4"); //類型//創建一個 newType,這個新Type,充當Type 來使用Type newType = type.MakeGenericType(new Type[] { typeof(Int32), typeof(DateTime), typeof(String), typeof(Int32) });object o1 = Activator.CreateInstance(newType);//創建實例MethodInfo method = newType.GetMethod("Show");method.Invoke(o1,new object[] {12,DateTime.Now, "12", 12 });} 先加載DLL →?獲取類型Type →?再創建一個指定泛型的新Type →?再創建實例 → 用新Type創建方法 →??再調用方法 2.泛型類反射(類型泛型數少于方法泛型數) public static void _ShowGeneric(){Assembly assembly1 = Assembly.Load("ClassDemo1");Type type = assembly1.GetType("ClassDemo1.GenericTest`4");Type newType = type.MakeGenericType(new Type[] { typeof(Int32), typeof(Int32), typeof(Int32), typeof(Int32) });object o = Activator.CreateInstance(newType);MethodInfo method = newType.GetMethod("_Show");MethodInfo newMethod = method.MakeGenericMethod(new Type[] { typeof(string), typeof(string) });newMethod.Invoke(o, new object[] { 12,12,12,12, "12", "12" });} 先加載DLL →?獲取類型Type →?再創建一個指定泛型的新Type →?再創建實例 → 用新Type創建方法 →?再創建一個指定泛型的新Method →??用新的method調用方法3.獲取屬性,操作字段,獲取字段 public static void ActionProperties(){Assembly assembly1 = Assembly.Load("ClassDemo1");Type type = assembly1.GetType("ClassDemo1.UserInfo");object o = Activator.CreateInstance(type);foreach (var item in type.GetProperties()){if (String.Equals(item.Name, "Id")){item.SetValue(o, 123);}if (String.Equals(item.Name, "Name")){item.SetValue(o, "零");}if (String.Equals(item.Name, "Age")){item.SetValue(o, 27);}if (String.Equals(item.Name, "Date")){item.SetValue(o, DateTime.Now);}Console.WriteLine(item.GetValue(o));}}
4.獲取字段,操作字段,獲取字段
public static void ActionFields(){Assembly assembly1 = Assembly.Load("ClassDemo1");Type type = assembly1.GetType("ClassDemo1.UserInfo");object o = Activator.CreateInstance(type);foreach (var item in type.GetFields()){if (String.Equals(item.Name, "InputDate")){item.SetValue(o, DateTime.Now);}Console.WriteLine(item.GetValue(o));}}?
4.1 獲取私有的字段
var item1 = type.GetField("JobName", BindingFlags.Instance | BindingFlags.NonPublic);10.反射字段或屬性的實踐,兩個對象字段的轉移
public static void UseActionFields(){UserInfo user = new UserInfo(){ Id = 12,Name = "零",Age = 27,Date = DateTime.Now,InputDate=DateTime.Now,};Assembly assembly1 = Assembly.Load("ClassDemo1");Type type = assembly1.GetType("ClassDemo1.UserInfo");object o = Activator.CreateInstance(type);foreach (var item in type.GetProperties())//屬性 {item.SetValue(o, typeof(UserInfo).GetProperty(item.Name).GetValue(user));//根據實體的Type獲取具體屬性的值Console.WriteLine(item.GetValue(o));//獲取屬性值 }foreach (var item in type.GetFields())//字段 {item.SetValue(o, typeof(UserInfo).GetField(item.Name).GetValue(user));//根據實體的Type獲取具體字段的值Console.WriteLine(item.GetValue(o));//獲取字段值 }}11.優點:「動態」
? ? ?確定:1,寫起來復雜
?2,避開編譯器檢查
?3,性能問題;100W 性能差別可能有400 - 500 倍,但絕對值很小,絕大情況下不會影響性能,
但可以空間換時間,緩存
MVC,EF,都是第一次很慢,吧所有的公共方法加載到緩存,后面就很快
轉載于:https://www.cnblogs.com/Jacob-Wu/p/9291611.html
總結
以上是生活随笔為你收集整理的反射(操作MetaData)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CentOS 7 源码编译安装 Redi
- 下一篇: Metail Design各个控件(二)