日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

反射(操作MetaData)

發布時間:2025/6/17 编程问答 66 豆豆
生活随笔 收集整理的這篇文章主要介紹了 反射(操作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)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。