反射(操作MetaData)
生活随笔
收集整理的這篇文章主要介紹了
反射(操作MetaData)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.泛型類(lèi)反射(類(lèi)型泛型數(shù)和方法泛型數(shù)一致)
public static void ShowGeneric(){Assembly a1 = Assembly.Load("ClassDemo1");//DLLType type = a1.GetType("ClassDemo1.GenericTest`4"); //類(lèi)型//創(chuàng)建一個(gè) newType,這個(gè)新Type,充當(dāng)Type 來(lái)使用Type newType = type.MakeGenericType(new Type[] { typeof(Int32), typeof(DateTime), typeof(String), typeof(Int32) });object o1 = Activator.CreateInstance(newType);//創(chuàng)建實(shí)例MethodInfo method = newType.GetMethod("Show");method.Invoke(o1,new object[] {12,DateTime.Now, "12", 12 });} 先加載DLL →?獲取類(lèi)型Type →?再創(chuàng)建一個(gè)指定泛型的新Type →?再創(chuàng)建實(shí)例 → 用新Type創(chuàng)建方法 →??再調(diào)用方法 2.泛型類(lèi)反射(類(lèi)型泛型數(shù)少于方法泛型數(shù)) 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 →?獲取類(lèi)型Type →?再創(chuàng)建一個(gè)指定泛型的新Type →?再創(chuàng)建實(shí)例 → 用新Type創(chuàng)建方法 →?再創(chuàng)建一個(gè)指定泛型的新Method →??用新的method調(diào)用方法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.反射字段或?qū)傩缘膶?shí)踐,兩個(gè)對(duì)象字段的轉(zhuǎn)移
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));//根據(jù)實(shí)體的Type獲取具體屬性的值Console.WriteLine(item.GetValue(o));//獲取屬性值 }foreach (var item in type.GetFields())//字段 {item.SetValue(o, typeof(UserInfo).GetField(item.Name).GetValue(user));//根據(jù)實(shí)體的Type獲取具體字段的值Console.WriteLine(item.GetValue(o));//獲取字段值 }}11.優(yōu)點(diǎn):「動(dòng)態(tài)」
? ? ?確定:1,寫(xiě)起來(lái)復(fù)雜
?2,避開(kāi)編譯器檢查
?3,性能問(wèn)題;100W 性能差別可能有400 - 500 倍,但絕對(duì)值很小,絕大情況下不會(huì)影響性能,
但可以空間換時(shí)間,緩存
MVC,EF,都是第一次很慢,吧所有的公共方法加載到緩存,后面就很快
轉(zhuǎn)載于:https://www.cnblogs.com/Jacob-Wu/p/9291611.html
總結(jié)
以上是生活随笔為你收集整理的反射(操作MetaData)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: CentOS 7 源码编译安装 Redi
- 下一篇: Metail Design各个控件(二)