97色伦色在线综合视频,无玛专区,18videosex性欧美黑色,日韩黄色电影免费在线观看,国产精品伦理一区二区三区,在线视频欧美日韩,亚洲欧美在线中文字幕不卡

注冊一個小網(wǎng)站銷售客戶管理軟件哪個好

鶴壁市浩天電氣有限公司 2026/01/24 11:14:05
注冊一個小網(wǎng)站,銷售客戶管理軟件哪個好,建設(shè)營銷網(wǎng)站,網(wǎng)絡(luò)運(yùn)維工程師招聘要求反射#xff08;Reflection#xff09;是 C# 的 “元編程” 能力#xff0c;允許程序在運(yùn)行時獲取類型信息、調(diào)用方法、創(chuàng)建實例#xff1b;泛型#xff08;Generic#xff09;則是 “類型參數(shù)化”#xff0c;實現(xiàn)類型安全的代碼復(fù)用。兩者結(jié)合可突破靜態(tài)泛型的限制Reflection是 C# 的 “元編程” 能力允許程序在運(yùn)行時獲取類型信息、調(diào)用方法、創(chuàng)建實例泛型Generic則是 “類型參數(shù)化”實現(xiàn)類型安全的代碼復(fù)用。兩者結(jié)合可突破靜態(tài)泛型的限制實現(xiàn)動態(tài)綁定泛型參數(shù)、調(diào)用泛型方法、操作泛型成員是框架開發(fā)如 DI 容器、ORM、序列化的核心技術(shù)。本文將從 “基礎(chǔ)概念→核心 API→分場景超詳細(xì)案例→底層原理→避坑指南” 全維度講解所有案例均為可直接運(yùn)行的控制臺程序每一行代碼都附帶注釋和原理說明。一、核心概念前置在開始案例前先明確反射操作泛型的核心術(shù)語避免概念混淆術(shù)語定義示例泛型定義未綁定類型未指定具體泛型參數(shù)的泛型類型 / 方法僅表示 “模板”無法直接實例化 / 調(diào)用typeof(GenericClass)、MethodInfo泛型方法定義已綁定類型 / 方法綁定了具體泛型參數(shù)的泛型類型 / 方法可實例化 / 調(diào)用typeof(GenericClassint)、綁定了string的泛型方法泛型參數(shù)Type Argument綁定到泛型定義的具體類型如 int、stringMakeGenericType(typeof(int))中的 int泛型形參Type Parameter泛型定義中的占位符如T、Uclass GenericClassT中的 T二、核心 API 全解析反射操作泛型的 “工具箱”以下是反射操作泛型的核心 API按 “類型操作→方法操作→成員操作” 分類每個 API 附帶用途和使用場景2.1 泛型類型操作 APIAPI用途Type.IsGenericType判斷類型是否是已綁定的泛型類型如GenericClassint返回 trueType.IsGenericTypeDefinition判斷類型是否是泛型定義如GenericClass返回 trueType.GetGenericTypeDefinition()從已綁定類型獲取泛型定義如GenericClassint→GenericClassType.MakeGenericType(params Type[])綁定泛型參數(shù)從泛型定義生成已綁定類型核心 APIType.GetGenericArguments()獲取泛型參數(shù)數(shù)組已綁定類型返回具體類型泛型定義返回形參2.2 泛型方法操作 APIAPI用途MethodInfo.IsGenericMethod判斷方法是否是泛型方法已綁定 / 未綁定均返回 trueMethodInfo.IsGenericMethodDefinition判斷方法是否是泛型方法定義未綁定參數(shù)MethodInfo.MakeGenericMethod(params Type[])綁定泛型參數(shù)從泛型方法定義生成已綁定方法核心 APIMethodInfo.GetGenericArguments()獲取方法的泛型參數(shù)數(shù)組2.3 實例 / 成員操作 APIAPI用途Activator.CreateInstance(Type, params object[])創(chuàng)建泛型類型實例支持無參 / 帶參構(gòu)造MethodInfo.Invoke(object, object[])調(diào)用泛型方法實例方法傳實例靜態(tài)方法傳 nullPropertyInfo.GetValue(object)/SetValue(object)讀寫泛型類型的屬性三、控制臺案例分 8 個核心場景所有案例基于同一控制臺項目先定義測試用的泛型類型類、接口、方法再分場景演示反射操作。3.0 準(zhǔn)備測試用泛型類型基礎(chǔ)依賴先定義一套覆蓋 “泛型類、泛型接口、泛型方法實例 / 靜態(tài) / 重載、泛型屬性 / 字段” 的測試類型后續(xù)所有反射操作均基于此using System; using System.Collections.Generic; using System.Reflection; namespace ReflectionGenericUltraDetail { #region 測試用泛型類型定義核心依賴 /// summary /// 泛型測試類包含泛型字段、屬性、構(gòu)造函數(shù)、實例泛型方法、靜態(tài)泛型方法、重載泛型方法 /// /summary /// typeparam nameT主泛型參數(shù)/typeparam public class GenericTestClassT { // 泛型字段 public T GenericField; // 泛型屬性 public T GenericProperty { get; set; } // 非泛型屬性對比 public int NonGenericProperty { get; set; } 0; #region 構(gòu)造函數(shù)無參/單參/多參 /// summary /// 無參構(gòu)造函數(shù) /// /summary public GenericTestClass() { Console.WriteLine($【構(gòu)造函數(shù)】GenericTestClass{typeof(T).Name} 無參構(gòu)造執(zhí)行); } /// summary /// 單參構(gòu)造函數(shù)泛型參數(shù)類型 /// /summary /// param nameinitValue泛型類型的初始值/param public GenericTestClass(T initValue) { GenericField initValue; GenericProperty initValue; Console.WriteLine($【構(gòu)造函數(shù)】GenericTestClass{typeof(T).Name} 單參構(gòu)造執(zhí)行初始值{initValue}); } /// summary /// 多參構(gòu)造函數(shù)泛型非泛型參數(shù) /// /summary /// param nameinitValue泛型初始值/param /// param namenonGenericValue非泛型int值/param public GenericTestClass(T initValue, int nonGenericValue) { GenericField initValue; GenericProperty initValue; NonGenericProperty nonGenericValue; Console.WriteLine($【構(gòu)造函數(shù)】GenericTestClass{typeof(T).Name} 多參構(gòu)造執(zhí)行泛型值{initValue}非泛型值{nonGenericValue}); } #endregion #region 實例方法泛型/非泛型/重載 /// summary /// 非泛型實例方法 /// /summary public void NonGenericInstanceMethod() { Console.WriteLine($【實例方法】非泛型方法執(zhí)行泛型類型{typeof(T).Name}GenericProperty值{GenericProperty}); } /// summary /// 泛型實例方法單泛型參數(shù) /// /summary /// typeparam nameU方法級泛型參數(shù)/typeparam /// param nameinput1類泛型參數(shù)T類型/param /// param nameinput2方法泛型參數(shù)U類型/param public void GenericInstanceMethodU(T input1, U input2) { Console.WriteLine($【實例方法】泛型方法{typeof(U).Name}執(zhí)行輸入1{typeof(T).Name}{input1}輸入2{typeof(U).Name}{input2}); } /// summary /// 泛型實例方法重載版 /// /summary /// typeparam nameU方法級泛型參數(shù)/typeparam /// typeparam nameV第二個方法級泛型參數(shù)/typeparam /// param nameinput1T類型/param /// param nameinput2U類型/param /// param nameinput3V類型/param public void GenericInstanceMethodU, V(T input1, U input2, V input3) { Console.WriteLine($【實例方法】泛型方法{typeof(U).Name},{typeof(V).Name}重載版執(zhí)行輸入1{input1}輸入2{input2}輸入3{input3}); } #endregion #region 靜態(tài)方法泛型/非泛型 /// summary /// 非泛型靜態(tài)方法 /// /summary /// param namemessage字符串參數(shù)/param public static void NonGenericStaticMethod(string message) { Console.WriteLine($【靜態(tài)方法】非泛型靜態(tài)方法執(zhí)行消息{message}); } /// summary /// 泛型靜態(tài)方法 /// /summary /// typeparam nameW靜態(tài)方法泛型參數(shù)/typeparam /// param namevalueW類型參數(shù)/param public static void GenericStaticMethodW(W value) { Console.WriteLine($【靜態(tài)方法】泛型靜態(tài)方法{typeof(W).Name}執(zhí)行值{value}); } #endregion } /// summary /// 泛型接口測試泛型接口反射 /// /summary /// typeparam nameT接口泛型參數(shù)/typeparam public interface IGenericTestInterfaceT { T GetData(); void SetData(T data); void GenericInterfaceMethodU(T input, U output); } /// summary /// 泛型接口實現(xiàn)類 /// /summary /// typeparam nameT實現(xiàn)類泛型參數(shù)/typeparam public class GenericInterfaceImplementT : IGenericTestInterfaceT { private T _data; public T GetData() { Console.WriteLine($【接口實現(xiàn)】GetData()執(zhí)行返回值{_data}); return _data; } public void SetData(T data) { _data data; Console.WriteLine($【接口實現(xiàn)】SetData()執(zhí)行設(shè)置值{data}); } public void GenericInterfaceMethodU(T input, U output) { Console.WriteLine($【接口實現(xiàn)】泛型接口方法{typeof(U).Name}執(zhí)行輸入{input}輸出{output}); } } #endregion class Program { static void Main(string[] args) { Console.WriteLine( 場景1泛型類型的反射基礎(chǔ)操作 ); Scene1_GenericTypeBasicOperations(); Console.WriteLine( 場景2動態(tài)創(chuàng)建泛型類實例全構(gòu)造函數(shù) ); Scene2_CreateGenericInstance(); Console.WriteLine( 場景3動態(tài)調(diào)用泛型實例方法含重載 ); Scene3_CallGenericInstanceMethod(); Console.WriteLine( 場景4動態(tài)調(diào)用泛型靜態(tài)方法 ); Scene4_CallGenericStaticMethod(); Console.WriteLine( 場景5反射操作泛型字段/屬性 ); Scene5_ReflectGenericFieldProperty(); Console.WriteLine( 場景6泛型接口的反射操作 ); Scene6_ReflectGenericInterface(); Console.WriteLine( 場景7嵌套泛型的反射處理 ); Scene7_HandleNestedGeneric(); Console.WriteLine( 場景8泛型方法重載的精準(zhǔn)獲取 ); Scene8_GetOverloadGenericMethod(); Console.ReadKey(); }3.1 場景 1泛型類型的反射基礎(chǔ)操作目標(biāo)掌握 “泛型定義” 與 “已綁定類型” 的區(qū)分、核心屬性判斷、泛型參數(shù)獲取。/// summary /// 場景1泛型類型的反射基礎(chǔ)操作泛型定義/已綁定類型區(qū)分、參數(shù)獲取 /// /summary static void Scene1_GenericTypeBasicOperations() { // 1. 獲取泛型定義未綁定類型typeof(類名)注意內(nèi)無參數(shù) Type genericDefinition typeof(GenericTestClass); Console.WriteLine($1. 泛型定義類型名{genericDefinition.FullName}); Console.WriteLine($ - 是否是泛型定義{genericDefinition.IsGenericTypeDefinition}); Console.WriteLine($ - 是否是泛型類型{genericDefinition.IsGenericType}); // 泛型定義也是泛型類型 Console.WriteLine($ - 泛型形參數(shù)量{genericDefinition.GetGenericArguments().Length}); // 1個形參T Console.WriteLine($ - 泛型形參名稱{genericDefinition.GetGenericArguments()[0].Name}); // T // 2. 綁定泛型參數(shù)生成已綁定類型核心MakeGenericType Type boundType_Int genericDefinition.MakeGenericType(typeof(int)); Console.WriteLine($ 2. 綁定int后的已綁定類型名{boundType_Int.FullName}); Console.WriteLine($ - 是否是泛型定義{boundType_Int.IsGenericTypeDefinition}); // false已綁定 Console.WriteLine($ - 是否是泛型類型{boundType_Int.IsGenericType}); // true Console.WriteLine($ - 泛型實參類型{boundType_Int.GetGenericArguments()[0].Name}); // Int32具體類型 // 3. 從已綁定類型反推泛型定義GetGenericTypeDefinition Type derivedDefinition boundType_Int.GetGenericTypeDefinition(); Console.WriteLine($ 3. 從已綁定類型反推的泛型定義{derivedDefinition.FullName}); Console.WriteLine($ - 與原泛型定義是否相同{derivedDefinition genericDefinition}); // true // 4. 非泛型類型的對比驗證屬性 Type nonGenericType typeof(string); Console.WriteLine($ 4. 非泛型類型string); Console.WriteLine($ - 是否是泛型類型{nonGenericType.IsGenericType}); // false Console.WriteLine($ - 是否是泛型定義{nonGenericType.IsGenericTypeDefinition}); // false }運(yùn)行結(jié)果1. 泛型定義類型名ReflectionGenericUltraDetail.GenericTestClass1 - 是否是泛型定義True - 是否是泛型類型True - 泛型形參數(shù)量1 - 泛型形參名稱T 2. 綁定int后的已綁定類型名ReflectionGenericUltraDetail.GenericTestClass1[[System.Int32, System.Private.CoreLib, Version7.0.0.0, Cultureneutral, PublicKeyToken7cec85d7bea7798e]] - 是否是泛型定義False - 是否是泛型類型True - 泛型實參類型Int32 3. 從已綁定類型反推的泛型定義ReflectionGenericUltraDetail.GenericTestClass1 - 與原泛型定義是否相同True 4. 非泛型類型string - 是否是泛型類型False - 是否是泛型定義False核心說明泛型定義是 “模板”無法直接實例化Activator.CreateInstance(typeof(GenericTestClass))會報錯必須通過MakeGenericType綁定具體類型生成 “已綁定類型” 后才能實例化 / 調(diào)用方法GetGenericTypeDefinition()是 “反向操作”可從已綁定類型還原泛型定義用于緩存泛型模板。3.2 場景 2動態(tài)創(chuàng)建泛型類實例全構(gòu)造函數(shù)目標(biāo)掌握無參、單參、多參構(gòu)造函數(shù)的泛型實例創(chuàng)建解決 “構(gòu)造函數(shù)參數(shù)匹配” 問題。/// summary /// 場景2動態(tài)創(chuàng)建泛型類實例無參/單參/多參構(gòu)造函數(shù) /// /summary static void Scene2_CreateGenericInstance() { // 步驟1獲取泛型定義并綁定int類型已綁定類型 Type genericDefinition typeof(GenericTestClass); Type boundType_Int genericDefinition.MakeGenericType(typeof(int)); // 子場景1無參構(gòu)造創(chuàng)建實例 Console.WriteLine(【子場景1】無參構(gòu)造創(chuàng)建實例); object instance_NoParam Activator.CreateInstance(boundType_Int); Console.WriteLine($ - 實例類型{instance_NoParam.GetType().FullName}); Console.WriteLine($ - 轉(zhuǎn)換為強(qiáng)類型{instance_NoParam is GenericTestClassint}); // true // 子場景2單參構(gòu)造創(chuàng)建實例參數(shù)為泛型類型 Console.WriteLine( 【子場景2】單參構(gòu)造創(chuàng)建實例int類型參數(shù)99); object instance_SingleParam Activator.CreateInstance(boundType_Int, 99); // 第二個參數(shù)是構(gòu)造函數(shù)參數(shù)數(shù)組 // 驗證獲取GenericProperty屬性值后續(xù)場景會詳解屬性反射此處先強(qiáng)轉(zhuǎn) GenericTestClassint strongType_SingleParam instance_SingleParam as GenericTestClassint; Console.WriteLine($ - 實例的GenericProperty值{strongType_SingleParam.GenericProperty}); // 99 // 子場景3多參構(gòu)造創(chuàng)建實例泛型參數(shù)非泛型參數(shù) Console.WriteLine( 【子場景3】多參構(gòu)造創(chuàng)建實例int88非泛型int1000); object instance_MultiParam Activator.CreateInstance(boundType_Int, 88, 1000); GenericTestClassint strongType_MultiParam instance_MultiParam as GenericTestClassint; Console.WriteLine($ - GenericProperty值{strongType_MultiParam.GenericProperty}); // 88 Console.WriteLine($ - NonGenericProperty值{strongType_MultiParam.NonGenericProperty}); // 1000 // 擴(kuò)展綁定string類型創(chuàng)建單參實例 Console.WriteLine( 【擴(kuò)展】綁定string類型單參構(gòu)造參數(shù)Hello Reflection); Type boundType_String genericDefinition.MakeGenericType(typeof(string)); object instance_String Activator.CreateInstance(boundType_String, Hello Reflection); GenericTestClassstring strongType_String instance_String as GenericTestClassstring; Console.WriteLine($ - String類型實例的GenericProperty{strongType_String.GenericProperty}); // 異常場景演示構(gòu)造函數(shù)參數(shù)類型不匹配 try { Console.WriteLine( 【異常演示】構(gòu)造函數(shù)參數(shù)類型不匹配int類型實例傳string參數(shù)); Activator.CreateInstance(boundType_Int, 錯誤的字符串參數(shù)); } catch (TargetInvocationException ex) { Console.WriteLine($ - 異常信息{ex.InnerException.Message}); // 無法將string轉(zhuǎn)換為int } }運(yùn)行結(jié)果【子場景1】無參構(gòu)造創(chuàng)建實例 【構(gòu)造函數(shù)】GenericTestClassInt32 無參構(gòu)造執(zhí)行 - 實例類型ReflectionGenericUltraDetail.GenericTestClass1[[System.Int32, System.Private.CoreLib, Version7.0.0.0, Cultureneutral, PublicKeyToken7cec85d7bea7798e]] - 轉(zhuǎn)換為強(qiáng)類型True 【子場景2】單參構(gòu)造創(chuàng)建實例int類型參數(shù)99 【構(gòu)造函數(shù)】GenericTestClassInt32 單參構(gòu)造執(zhí)行初始值99 - 實例的GenericProperty值99 【子場景3】多參構(gòu)造創(chuàng)建實例int88非泛型int1000 【構(gòu)造函數(shù)】GenericTestClassInt32 多參構(gòu)造執(zhí)行泛型值88非泛型值1000 - GenericProperty值88 - NonGenericProperty值1000 【擴(kuò)展】綁定string類型單參構(gòu)造參數(shù)Hello Reflection 【構(gòu)造函數(shù)】GenericTestClassString 單參構(gòu)造執(zhí)行初始值Hello Reflection - String類型實例的GenericPropertyHello Reflection 【異常演示】構(gòu)造函數(shù)參數(shù)類型不匹配int類型實例傳string參數(shù) - 異常信息Object of type System.String cannot be converted to type System.Int32.核心說明Activator.CreateInstance(Type, params object[])是創(chuàng)建實例的核心方法第二個參數(shù)是構(gòu)造函數(shù)的參數(shù)數(shù)組構(gòu)造函數(shù)參數(shù)類型必須與已綁定類型的泛型參數(shù)匹配否則會拋出TargetInvocationException內(nèi)層是類型轉(zhuǎn)換異常強(qiáng)轉(zhuǎn)as GenericTestClassint是反射后使用實例的常用方式前提是知道泛型參數(shù)類型。3.3 場景 3動態(tài)調(diào)用泛型實例方法含重載目標(biāo)掌握 “泛型方法定義” 的獲取、方法級泛型參數(shù)的綁定、實例方法的調(diào)用。/// summary /// 場景3動態(tài)調(diào)用泛型實例方法含方法級泛型參數(shù)綁定 /// /summary static void Scene3_CallGenericInstanceMethod() { // 步驟1創(chuàng)建泛型類實例綁定int類型單參構(gòu)造100 Type boundType_Int typeof(GenericTestClassint); object instance Activator.CreateInstance(boundType_Int, 100); // 子場景1調(diào)用非泛型實例方法基礎(chǔ) Console.WriteLine(【子場景1】調(diào)用非泛型實例方法); MethodInfo nonGenericMethod boundType_Int.GetMethod(NonGenericInstanceMethod); nonGenericMethod.Invoke(instance, null); // 第二個參數(shù)方法參數(shù)數(shù)組無參則傳null // 子場景2調(diào)用泛型實例方法單泛型參數(shù)U Console.WriteLine( 【子場景2】調(diào)用泛型實例方法GenericInstanceMethodU綁定U為string); // 步驟1獲取泛型方法定義未綁定方法 MethodInfo genericMethodDef boundType_Int.GetMethod(GenericInstanceMethod); Console.WriteLine($ - 方法是否是泛型定義{genericMethodDef.IsGenericMethodDefinition}); // true Console.WriteLine($ - 方法泛型形參數(shù)量{genericMethodDef.GetGenericArguments().Length}); // 1U // 步驟2綁定方法級泛型參數(shù)U→string MethodInfo boundMethod genericMethodDef.MakeGenericMethod(typeof(string)); Console.WriteLine($ - 綁定后的方法名{boundMethod.Name}); // 步驟3調(diào)用方法參數(shù)數(shù)組input1100Tintinput2測試字符串Ustring boundMethod.Invoke(instance, new object[] { 100, 測試字符串 }); // 子場景3調(diào)用重載的泛型實例方法UV兩個泛型參數(shù) Console.WriteLine( 【子場景3】調(diào)用重載泛型實例方法GenericInstanceMethodU,VUdoubleVDateTime); // 注意GetMethod默認(rèn)獲取第一個匹配名稱的方法重載需指定參數(shù)類型場景8詳解 MethodInfo genericMethodDef_Overload boundType_Int.GetMethod( GenericInstanceMethod, new Type[] { typeof(int), typeof(object), typeof(object) } // 參數(shù)類型Tint, Uobject, Vobject臨時占位 ); // 綁定方法泛型參數(shù)UdoubleVDateTime MethodInfo boundMethod_Overload genericMethodDef_Overload.MakeGenericMethod(typeof(double), typeof(DateTime)); // 調(diào)用方法input1200intinput23.14doubleinput3當(dāng)前時間DateTime boundMethod_Overload.Invoke(instance, new object[] { 200, 3.14, DateTime.Now }); }運(yùn)行結(jié)果【構(gòu)造函數(shù)】GenericTestClassInt32 單參構(gòu)造執(zhí)行初始值100 【子場景1】調(diào)用非泛型實例方法 【實例方法】非泛型方法執(zhí)行泛型類型Int32GenericProperty值100 【子場景2】調(diào)用泛型實例方法GenericInstanceMethodU綁定U為string - 方法是否是泛型定義True - 方法泛型形參數(shù)量1 - 綁定后的方法名GenericInstanceMethod 【實例方法】泛型方法String執(zhí)行輸入1Int32100輸入2String測試字符串 【子場景3】調(diào)用重載泛型實例方法GenericInstanceMethodU,VUdoubleVDateTime 【實例方法】泛型方法Double,DateTime重載版執(zhí)行輸入1200輸入23.14輸入32025/12/16 15:30:00核心說明泛型方法的操作分三步獲取方法定義→綁定方法級泛型參數(shù)→調(diào)用InvokeMethodInfo.Invoke(object instance, object[] parameters)第一個參數(shù)實例方法傳實例靜態(tài)方法傳null第二個參數(shù)方法參數(shù)數(shù)組順序必須與方法定義一致重載方法的精準(zhǔn)獲取需指定參數(shù)類型場景 8 詳解。3.4 場景 4動態(tài)調(diào)用泛型靜態(tài)方法目標(biāo)掌握靜態(tài)泛型方法的獲取、綁定、調(diào)用無需實例。/// summary /// 場景4動態(tài)調(diào)用泛型靜態(tài)方法無需創(chuàng)建實例 /// /summary static void Scene4_CallGenericStaticMethod() { // 步驟1獲取已綁定類型int Type boundType_Int typeof(GenericTestClassint); // 子場景1調(diào)用非泛型靜態(tài)方法 Console.WriteLine(【子場景1】調(diào)用非泛型靜態(tài)方法); MethodInfo nonGenericStaticMethod boundType_Int.GetMethod(NonGenericStaticMethod); nonGenericStaticMethod.Invoke(null, new object[] { 靜態(tài)方法測試消息 }); // 實例參數(shù)傳null // 子場景2調(diào)用泛型靜態(tài)方法 Console.WriteLine( 【子場景2】調(diào)用泛型靜態(tài)方法GenericStaticMethodW綁定W為bool); // 步驟1獲取泛型靜態(tài)方法定義 MethodInfo genericStaticMethodDef boundType_Int.GetMethod(GenericStaticMethod); Console.WriteLine($ - 是否是泛型方法定義{genericStaticMethodDef.IsGenericMethodDefinition}); // true // 步驟2綁定方法泛型參數(shù)W→bool MethodInfo boundStaticMethod genericStaticMethodDef.MakeGenericMethod(typeof(bool)); // 步驟3調(diào)用靜態(tài)方法實例參數(shù)null方法參數(shù)true boundStaticMethod.Invoke(null, new object[] { true }); // 擴(kuò)展綁定不同泛型參數(shù)WDateTime Console.WriteLine( 【擴(kuò)展】綁定WDateTime調(diào)用泛型靜態(tài)方法); MethodInfo boundStaticMethod_DateTime genericStaticMethodDef.MakeGenericMethod(typeof(DateTime)); boundStaticMethod_DateTime.Invoke(null, new object[] { DateTime.Now }); }運(yùn)行結(jié)果【子場景1】調(diào)用非泛型靜態(tài)方法 【靜態(tài)方法】非泛型靜態(tài)方法執(zhí)行消息靜態(tài)方法測試消息 【子場景2】調(diào)用泛型靜態(tài)方法GenericStaticMethodW綁定W為bool - 是否是泛型方法定義True 【靜態(tài)方法】泛型靜態(tài)方法Boolean執(zhí)行值True 【擴(kuò)展】綁定WDateTime調(diào)用泛型靜態(tài)方法 【靜態(tài)方法】泛型靜態(tài)方法DateTime執(zhí)行值2025/12/16 15:30:00核心說明靜態(tài)方法調(diào)用時Invoke的第一個參數(shù)必須傳null泛型靜態(tài)方法的綁定邏輯與實例泛型方法一致區(qū)別僅在于 “無需實例”靜態(tài)方法屬于類型本身與實例無關(guān)因此無需創(chuàng)建泛型類實例。3.5 場景 5反射操作泛型字段 / 屬性目標(biāo)掌握泛型類型的字段、屬性的讀取和修改解決 “動態(tài)訪問泛型成員” 問題。/// summary /// 場景5反射操作泛型字段/屬性讀取/修改 /// /summary static void Scene5_ReflectGenericFieldProperty() { // 步驟1創(chuàng)建泛型實例綁定string類型單參構(gòu)造初始值 Type boundType_String typeof(GenericTestClassstring); object instance Activator.CreateInstance(boundType_String, 初始值); // 子場景1操作泛型字段GenericField Console.WriteLine(【子場景1】操作泛型字段GenericField); FieldInfo genericField boundType_String.GetField(GenericField); // 讀取字段值 object fieldValue genericField.GetValue(instance); Console.WriteLine($ - 原始字段值{fieldValue}); // 修改字段值 genericField.SetValue(instance, 修改后的字段值); Console.WriteLine($ - 修改后的字段值{genericField.GetValue(instance)}); // 子場景2操作泛型屬性GenericProperty Console.WriteLine( 【子場景2】操作泛型屬性GenericProperty); PropertyInfo genericProperty boundType_String.GetProperty(GenericProperty); // 讀取屬性值 object propValue genericProperty.GetValue(instance); Console.WriteLine($ - 原始屬性值{propValue}); // 修改屬性值 genericProperty.SetValue(instance, 修改后的屬性值); Console.WriteLine($ - 修改后的屬性值{genericProperty.GetValue(instance)}); // 子場景3操作非泛型屬性NonGenericProperty Console.WriteLine( 【子場景3】操作非泛型屬性NonGenericProperty); PropertyInfo nonGenericProp boundType_String.GetProperty(NonGenericProperty); Console.WriteLine($ - 原始值{nonGenericProp.GetValue(instance)}); // 默認(rèn)0 nonGenericProp.SetValue(instance, 999); Console.WriteLine($ - 修改后值{nonGenericProp.GetValue(instance)}); // 999 // 異常場景字段/屬性不存在 try { Console.WriteLine( 【異常演示】獲取不存在的字段); boundType_String.GetField(不存在的字段); } catch (NullReferenceException ex) { Console.WriteLine($ - 異常{ex.Message}); } }運(yùn)行結(jié)果【構(gòu)造函數(shù)】GenericTestClassString 單參構(gòu)造執(zhí)行初始值初始值 【子場景1】操作泛型字段GenericField - 原始字段值初始值 - 修改后的字段值修改后的字段值 【子場景2】操作泛型屬性GenericProperty - 原始屬性值初始值 - 修改后的屬性值修改后的屬性值 【子場景3】操作非泛型屬性NonGenericProperty - 原始值0 - 修改后值999 【異常演示】獲取不存在的字段 - 異常Object reference not set to an instance of an object.核心說明FieldInfo.GetValue(object)/SetValue(object, object)讀取 / 修改字段值參數(shù)為實例和新值PropertyInfo.GetValue(object)/SetValue(object, object)讀取 / 修改屬性值邏輯與字段一致泛型字段 / 屬性的類型由已綁定類型的泛型參數(shù)決定如綁定 string 則字段類型為 string獲取成員時名稱必須完全匹配區(qū)分大小寫否則返回null調(diào)用GetValue會拋空引用異常。3.6 場景 6泛型接口的反射操作目標(biāo)掌握泛型接口的獲取、接口方法的調(diào)用、實現(xiàn)類的反射。/// summary /// 場景6泛型接口的反射操作獲取接口、調(diào)用接口方法 /// /summary static void Scene6_ReflectGenericInterface() { // 步驟1獲取泛型接口實現(xiàn)類的已綁定類型綁定double Type implementType_Double typeof(GenericInterfaceImplementdouble); // 子場景1檢查實現(xiàn)類是否實現(xiàn)泛型接口 Console.WriteLine(【子場景1】檢查實現(xiàn)類是否實現(xiàn)泛型接口); // 獲取接口類型GetInterface(接口名泛型參數(shù)個數(shù))1表示1個泛型參數(shù) Type genericInterfaceType implementType_Double.GetInterface(IGenericTestInterface1); Console.WriteLine($ - 是否實現(xiàn)IGenericTestInterfaceT{genericInterfaceType ! null}); // true Console.WriteLine($ - 接口泛型實參{genericInterfaceType.GetGenericArguments()[0].Name}); // Double // 子場景2創(chuàng)建實現(xiàn)類實例并調(diào)用接口方法 Console.WriteLine( 【子場景2】調(diào)用泛型接口方法); object instance Activator.CreateInstance(implementType_Double); // 調(diào)用SetData方法 MethodInfo setDataMethod genericInterfaceType.GetMethod(SetData); setDataMethod.Invoke(instance, new object[] { 3.1415926 }); // 參數(shù)double類型的π // 調(diào)用GetData方法 MethodInfo getDataMethod genericInterfaceType.GetMethod(GetData); object data getDataMethod.Invoke(instance, null); Console.WriteLine($ - GetData返回值{data}); // 調(diào)用接口的泛型方法GenericInterfaceMethodU Console.WriteLine( 【子場景3】調(diào)用接口的泛型方法); MethodInfo interfaceGenericMethodDef genericInterfaceType.GetMethod(GenericInterfaceMethod); // 綁定方法泛型參數(shù)U為string MethodInfo boundInterfaceMethod interfaceGenericMethodDef.MakeGenericMethod(typeof(string)); // 調(diào)用方法input2.718doubleoutput自然常數(shù)estring boundInterfaceMethod.Invoke(instance, new object[] { 2.718, 自然常數(shù)e }); }運(yùn)行結(jié)果【子場景1】檢查實現(xiàn)類是否實現(xiàn)泛型接口 - 是否實現(xiàn)IGenericTestInterfaceTTrue - 接口泛型實參Double 【子場景2】調(diào)用泛型接口方法 【接口實現(xiàn)】SetData()執(zhí)行設(shè)置值3.1415926 【接口實現(xiàn)】GetData()執(zhí)行返回值3.1415926 - GetData返回值3.1415926 【子場景3】調(diào)用接口的泛型方法 【接口實現(xiàn)】泛型接口方法String執(zhí)行輸入2.718輸出自然常數(shù)e核心說明獲取泛型接口需使用GetInterface(接口名泛型參數(shù)個數(shù) )如IGenericTestInterface11表示接口有 1 個泛型參數(shù)泛型接口的方法調(diào)用邏輯與泛型類方法一致區(qū)別在于 “方法定義來自接口類型”實現(xiàn)類的泛型參數(shù)會自動傳遞給接口的泛型參數(shù)如GenericInterfaceImplementdouble→IGenericTestInterfacedouble。3.7 場景 7嵌套泛型的反射處理目標(biāo)掌握嵌套泛型如ListDictionaryint, string的反射解決復(fù)雜泛型類型的操作問題。/// summary /// 場景7處理嵌套泛型如ListDictionaryint, string /// /summary static void Scene7_HandleNestedGeneric() { // 需求動態(tài)創(chuàng)建ListDictionaryint, string并添加元素 // 步驟1定義嵌套泛型的層級 // 內(nèi)層Dictionaryint, string Type dictGenericDef typeof(Dictionary,); // 2個泛型參數(shù) Type dictBoundType dictGenericDef.MakeGenericType(typeof(int), typeof(string)); // 外層ListDictionaryint, string Type listGenericDef typeof(List); // 1個泛型參數(shù) Type listBoundType listGenericDef.MakeGenericType(dictBoundType); Console.WriteLine($嵌套泛型類型名{listBoundType.FullName}); // 步驟2創(chuàng)建List實例 object listInstance Activator.CreateInstance(listBoundType); Console.WriteLine($List實例類型{listInstance.GetType().Name}); // 步驟3創(chuàng)建Dictionary實例并添加元素 object dictInstance Activator.CreateInstance(dictBoundType); // 調(diào)用Dictionary的Add方法key1valueApple MethodInfo dictAddMethod dictBoundType.GetMethod(Add); dictAddMethod.Invoke(dictInstance, new object[] { 1, Apple }); dictAddMethod.Invoke(dictInstance, new object[] { 2, Banana }); // 步驟4將Dictionary添加到List中 MethodInfo listAddMethod listBoundType.GetMethod(Add); listAddMethod.Invoke(listInstance, new object[] { dictInstance }); // 步驟5驗證List的Count屬性 PropertyInfo listCountProp listBoundType.GetProperty(Count); int count (int)listCountProp.GetValue(listInstance); Console.WriteLine($ListDictionaryint,string的元素個數(shù){count}); // 步驟6讀取Dictionary的元素擴(kuò)展 MethodInfo dictTryGetValueMethod dictBoundType.GetMethod(TryGetValue); object[] tryGetValueParams new object[] { 1, null }; // out參數(shù)初始為null bool success (bool)dictTryGetValueMethod.Invoke(dictInstance, tryGetValueParams); if (success) { Console.WriteLine($Dictionary中key1的值{tryGetValueParams[1]}); // out參數(shù)在索引1 } }運(yùn)行結(jié)果嵌套泛型類型名System.Collections.Generic.List1[[System.Collections.Generic.Dictionary2[[System.Int32, System.Private.CoreLib, Version7.0.0.0, Cultureneutral, PublicKeyToken7cec85d7bea7798e],[System.String, System.Private.CoreLib, Version7.0.0.0, Cultureneutral, PublicKeyToken7cec85d7bea7798e]], System.Private.CoreLib, Version7.0.0.0, Cultureneutral, PublicKeyToken7cec85d7bea7798e] List實例類型List1 ListDictionaryint,string的元素個數(shù)1 Dictionary中key1的值A(chǔ)pple核心說明嵌套泛型的處理需 “從內(nèi)到外” 綁定泛型參數(shù)先綁定內(nèi)層Dictionary再綁定外層Listout參數(shù)的處理調(diào)用方法時需傳入長度匹配的數(shù)組out參數(shù)的值會填充到數(shù)組對應(yīng)位置嵌套泛型是框架開發(fā)的高頻場景如 ORM 的IQueryableDictionarystring, object。3.8 場景 8泛型方法重載的精準(zhǔn)獲取目標(biāo)解決 “泛型方法重載時 GetMethod 獲取錯誤方法” 的問題掌握 “參數(shù)類型匹配” 的精準(zhǔn)獲取方式。/// summary /// 場景8精準(zhǔn)獲取重載的泛型方法解決GetMethod默認(rèn)獲取第一個匹配方法的問題 /// /summary static void Scene8_GetOverloadGenericMethod() { // 步驟1獲取已綁定類型int Type boundType_Int typeof(GenericTestClassint); // 問題GenericInstanceMethod有兩個重載1個參數(shù)泛型/2個參數(shù)泛型默認(rèn)GetMethod會獲取第一個 // 解決通過參數(shù)類型數(shù)組精準(zhǔn)匹配 Console.WriteLine(【子場景1】獲取單參數(shù)泛型方法GenericInstanceMethodU(T, U)); // 參數(shù)類型數(shù)組TintUobject方法參數(shù)的類型U是泛型參數(shù)臨時用object占位 Type[] singleParamMethodTypes new Type[] { typeof(int), typeof(object) }; MethodInfo singleParamGenericMethod boundType_Int.GetMethod( GenericInstanceMethod, singleParamMethodTypes ); Console.WriteLine($ - 方法是否是泛型定義{singleParamGenericMethod.IsGenericMethodDefinition}); Console.WriteLine($ - 方法參數(shù)個數(shù){singleParamGenericMethod.GetParameters().Length}); // 2個參數(shù)T, U // 綁定U為DateTime并調(diào)用 MethodInfo boundSingleMethod singleParamGenericMethod.MakeGenericMethod(typeof(DateTime)); object instance Activator.CreateInstance(boundType_Int, 500); boundSingleMethod.Invoke(instance, new object[] { 500, DateTime.Now }); Console.WriteLine( 【子場景2】獲取雙參數(shù)泛型方法GenericInstanceMethodU,V(T, U, V)); // 參數(shù)類型數(shù)組TintUobjectVobject Type[] doubleParamMethodTypes new Type[] { typeof(int), typeof(object), typeof(object) }; MethodInfo doubleParamGenericMethod boundType_Int.GetMethod( GenericInstanceMethod, doubleParamMethodTypes ); Console.WriteLine($ - 方法參數(shù)個數(shù){doubleParamGenericMethod.GetParameters().Length}); // 3個參數(shù)T, U, V // 綁定UfloatVGuid并調(diào)用 MethodInfo boundDoubleMethod doubleParamGenericMethod.MakeGenericMethod(typeof(float), typeof(Guid)); boundDoubleMethod.Invoke(instance, new object[] { 600, 1.23f, Guid.NewGuid() }); } } }運(yùn)行結(jié)果【子場景1】獲取單參數(shù)泛型方法GenericInstanceMethodU(T, U) - 方法是否是泛型定義True - 方法參數(shù)個數(shù)2 【構(gòu)造函數(shù)】GenericTestClassInt32 單參構(gòu)造執(zhí)行初始值500 【實例方法】泛型方法DateTime執(zhí)行輸入1Int32500輸入2DateTime2025/12/16 15:30:00 【子場景2】獲取雙參數(shù)泛型方法GenericInstanceMethodU,V(T, U, V) - 方法參數(shù)個數(shù)3 【實例方法】泛型方法Single,Guid重載版執(zhí)行輸入1600輸入21.23輸入3xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx核心說明泛型方法重載的區(qū)分核心是 “參數(shù)個數(shù) 參數(shù)類型”方法級泛型參數(shù)如 U、V在獲取方法時需用object占位綁定泛型參數(shù)后再傳入具體類型若重載方法的參數(shù)類型不同如intvsstring需精準(zhǔn)指定參數(shù)類型數(shù)組避免獲取錯誤方法。四、反射操作泛型的底層原理與性能優(yōu)化4.1 底層原理泛型類型在 CLR 中是 “模板生成”每個不同的泛型參數(shù)綁定如GenericClassint、GenericClassstring會生成獨立的 IL 代碼反射操作泛型的本質(zhì)是 “運(yùn)行時解析泛型元數(shù)據(jù)”CLR 維護(hù)了泛型類型的元數(shù)據(jù)泛型定義、參數(shù)、方法反射 API 通過讀取這些元數(shù)據(jù)實現(xiàn)動態(tài)操作MakeGenericType的底層是 “綁定泛型參數(shù)到元數(shù)據(jù)模板生成新的 Type 對象”。4.2 性能優(yōu)化反射操作的性能遠(yuǎn)低于靜態(tài)調(diào)用高頻場景需優(yōu)化緩存核心對象緩存Type、MethodInfo、PropertyInfo等對象避免重復(fù)調(diào)用GetType()/GetMethod()// 示例緩存泛型方法定義 private static readonly MethodInfo _cachedGenericMethod typeof(GenericTestClassint).GetMethod(GenericInstanceMethod);使用 Delegate 減少 Invoke 開銷將MethodInfo轉(zhuǎn)換為強(qiáng)類型委托避免每次Invoke的反射開銷避免頻繁創(chuàng)建實例復(fù)用泛型實例減少Activator.CreateInstance的調(diào)用使用表達(dá)式樹Expression替代反射對于高頻調(diào)用的泛型方法可通過表達(dá)式樹動態(tài)生成委托性能接近靜態(tài)調(diào)用。五、避坑指南常見錯誤與解決方案常見錯誤原因解決方案MakeGenericType傳入?yún)?shù)個數(shù)不匹配泛型定義的形參個數(shù)與傳入的實參個數(shù)不一致如T傳 2 個類型檢查typeof(類名)的泛型參數(shù)個數(shù)確保MakeGenericType參數(shù)個數(shù)匹配Invoke拋出TargetParameterCountException方法參數(shù)數(shù)組長度與方法定義的參數(shù)個數(shù)不一致核對方法參數(shù)個數(shù)確保Invoke的第二個參數(shù)數(shù)組長度匹配Invoke拋出ArgumentException參數(shù)類型與方法定義的參數(shù)類型不匹配如 int 傳 string確保參數(shù)類型與已綁定類型的泛型參數(shù)一致GetMethod返回null方法名錯誤 / 重載匹配失敗 / 非公共方法默認(rèn) GetMethod 只獲取公共方法檢查方法名大小寫 / 指定參數(shù)類型數(shù)組 / 使用BindingFlags獲取非公共方法泛型接口獲取失敗接口名未加泛型參數(shù)個數(shù)如IGenericInterface而非IGenericInterface1使用GetInterface(接口名參數(shù)個數(shù) )
版權(quán)聲明: 本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請聯(lián)系我們進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

建門戶網(wǎng)站需要多少錢怎么制作圖片相冊

建門戶網(wǎng)站需要多少錢,怎么制作圖片相冊,個體制作網(wǎng)站設(shè)計,蘭州網(wǎng)站設(shè)計哪個平臺好Gitee崛起#xff1a;2025年中國開發(fā)者為何選擇本土代碼托管平臺 在數(shù)字化轉(zhuǎn)型加速的2025年#xff0c;代碼

2026/01/22 21:48:01

北京泵網(wǎng)站建設(shè)114啦建站程序

北京泵網(wǎng)站建設(shè),114啦建站程序,做網(wǎng)站如何提需求,競價托管是什么意思資料查找方式#xff1a;特納斯電子#xff08;電子校園網(wǎng)#xff09;#xff1a;搜索下面編號即可編號#xff1a;T451

2026/01/22 23:26:01

手機(jī)網(wǎng)站列表 教程wordpress修頁面鏈接

手機(jī)網(wǎng)站列表 教程,wordpress修頁面鏈接,優(yōu)秀網(wǎng)站設(shè)計的標(biāo)準(zhǔn),wordpress全站ajax方法還在為抖音視頻上的水印煩惱嗎#xff1f;douyin_downloader 為你提供專業(yè)解決方

2026/01/23 07:14:01

seo網(wǎng)站程序網(wǎng)絡(luò)推廣營銷方法

seo網(wǎng)站程序,網(wǎng)絡(luò)推廣營銷方法,怎樣判斷網(wǎng)站的好壞,大連港健康打卡二維碼Langchain-Chatchat問答置信度評分機(jī)制設(shè)計 在企業(yè)級智能問答系統(tǒng)日益普及的今天#xff0c;一個看似流暢的回答

2026/01/23 05:41:01