1. 程式人生 > >[C#.NET 拾遺補漏]04:你必須知道的反射

[C#.NET 拾遺補漏]04:你必須知道的反射

閱讀本文大概需要 3 分鐘。
通常,反射用於動態獲取物件的型別、屬性和方法等資訊。今天帶你玩轉反射,來彙總一下反射的各種常見操作,撿漏看看有沒有你不知道的。 ## 獲取型別的成員 Type 類的 GetMembers 方法用來獲取該型別的所有成員,包括方法和屬性,可通過 BindingFlags 標誌來篩選這些成員。 ```csharp using System; using System.Reflection; using System.Linq; public class Program { public static voidMain() { var members = typeof(object).GetMembers(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance); foreach (var member in members) { Console.WriteLine($"{member.Name} is a {member.MemberType}"); } } } ``` 輸出: ```bash GetType is a Method GetHashCode is a Method ToString is a Method Equals is a Method ReferenceEquals is a Method .ctor is a Constructor ``` GetMembers 方法也可以不傳 BindingFlags,預設返回的是所有公開的成員。 ## 獲取並呼叫物件的方法 Type 型別的 GetMethod 方法用來獲取該型別的 MethodInfo,然後可通過 MethodInfo 動態呼叫該方法。 對於非靜態方法,需要傳遞對應的例項作為引數,示例: ```cs class Program { public static void Main() { var str = "hello"; var method = str.GetType() .GetMethod("Substring", new[] {typeof(int), typeof(int)}); var result = method.Invoke(str, new object[] {0, 4}); // 相當於 str.Substring(0, 4) Console.WriteLine(result); // 輸出:hell } } ``` 對於靜態方法,則物件引數傳空,示例: ```csharp var method = typeof(Math).GetMethod("Exp"); // 相當於 Math.Exp(2) var result = method.Invoke(null, new object[] {2}); Console.WriteLine(result); // 輸出(e^2):7.38905609893065 ``` 如果是泛型方法,則還需要通過泛型引數來建立泛型方法,示例: ```csharp class Program { public static void Main() { // 反射呼叫泛型方法 MethodInfo method1 = typeof(Sample).GetMethod("GenericMethod"); MethodInfo generic1 = method1.MakeGenericMethod(typeof(string)); generic1.Invoke(sample, null); // 反射呼叫靜態泛型方法 MethodInfo method2 = typeof(Sample).GetMethod("StaticMethod"); MethodInfo generic2 = method2.MakeGenericMethod(typeof(string)); generic2.Invoke(null, null); } } public class Sample { public void GenericMethod() { //... } public static void StaticMethod() { //... } } ``` ## 建立一個型別的例項 使用反射動態建立一個型別的例項有多種種方式。最簡單的一種是用 `new()` 條件宣告。 ### 使用 new 條件宣告 如果在一個方法內需要動態建立一個例項,可以直接使用 new 條件宣告,例如: ```csharp T GetInstance() where T : new() { T instance = newT(); return instance; } ``` 但這種方式適用場景有限,比如不適用於建構函式帶引數的型別。 ### 使用 Activator 類 使用 Activator 類動態建立一個類的例項是最常見的做法,示例: ```csharp Type type = typeof(BigInteger); object result = Activator.CreateInstance(type); Console.WriteLine(result); // 輸出:0 result = Activator.CreateInstance(type, 123); Console.WriteLine(result); // 輸出:123 ``` 動態建立泛型別例項,需要先建立開放泛型(如`List<>`),再根據泛型引數轉換為具象泛型(如`List`),示例: ```csharp // 先建立開放泛型 Type openType = typeof(List<>); // 再建立具象泛型 Type[] tArgs = { typeof(string) }; Type target = openType.MakeGenericType(tArgs); // 最後建立泛型例項 List result = (List)Activator.CreateInstance(target); ``` 如果你不知道什麼是開放泛型和具象泛型,請看本文最後一節。 ### 使用構造器反射 也可以通過反射構造器的方式動態建立類的例項,比上面使用 Activator 類要稍稍麻煩些,但效能要好些。示例: ```csharp ConstructorInfo c = typeof(T).GetConstructor(new[] { typeof(string) }); if (c == null) throw new InvalidOperationException("..."); T instance = (T)c.Invoke(new object[] { "test" }); ``` ### 使用 FormatterServices 類 如果你想建立某個類的例項的時候不執行建構函式和屬性初始化,可以使用 FormatterServices 的 GetUninitializedObject 方法。示例: ```csharp class Program { static void Main() { MyClass instance = (MyClass)FormatterServices.GetUninitializedObject(typeof(MyClass)); Console.WriteLine(instance.MyProperty1); // 輸出:0 Console.WriteLine(instance.MyProperty2); // 輸出:0 } } public class MyClass { public MyClass(int val) { MyProperty1 = val < 1 ? 1 : val; } public int MyProperty1 { get; } public int MyProperty2 { get; set; } = 2; } ``` ## 獲取屬性或方法的強型別委託 通過反射獲取到物件的屬性和方法後,如果你想通過強型別的方法來訪問或呼叫,可以在中間加一層委託。這樣的好處是有利於封裝,呼叫者可以明確的知道呼叫時需要傳什麼引數。 比如下面這個方法,把 Math.Max 方法提取為一個強型別委託: ```csharp var tArgs = new Type[] { typeof(int), typeof(int) }; var maxMethod = typeof(Math).GetMethod("Max", tArgs); var strongTypeDelegate = (Func)Delegate .CreateDelegate(typeof(Func), null, maxMethod); Console.WriteLine("3 和 5 之間最大的是:{0}", strongTypeDelegate(3, 5)); // 輸出:5 ``` 這個技巧也適用於屬性,可以獲取強型別的 Getter 和 Setter。示例: ```csharp var theProperty = typeof(MyClass).GetProperty("MyIntProperty"); // 強型別 Getter var theGetter = theProperty.GetGetMethod(); var strongTypeGetter = (Func)Delegate .CreateDelegate(typeof(Func), theGetter); var intVal = strongTypeGetter(target); // 相關於:target.MyIntProperty // 強型別 Setter var theSetter = theProperty.GetSetMethod(); var strongTypeSetter = (Action)Delegate .CreateDelegate(typeof(Action), theSetter); strongTypeSetter(target, 5); // 相當於:target.MyIntProperty = 5 ``` ## 反射獲取自定義特性 以下是四個常見的場景示例。 **示例一,**找出一個類中標註了某個自定義特性(比如 MyAtrribute)的屬性。 ```csharp var props = type .GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance) .Where(prop =>Attribute.IsDefined(prop, typeof(MyAttribute))); ``` **示例二,**找出某個屬性的所有自定義特性。 ```csharp var attributes = typeof(t).GetProperty("Name").GetCustomAttributes(false); ``` **示例三,**找出程式集所有標註了某個自定義特性的類。 ```csharp static IEnumerable GetTypesWithAttribute(Assembly assembly) { foreach(Type type inassembly.GetTypes()) { if (type.GetCustomAttributes(typeof(MyAttribute), true).Length > 0) { yield return type; } } } ``` **示例四,**在執行時讀取自定義特性的值 ```csharp public static class AttributeExtensions { public static TValue GetAttribute( this Type type, string MemberName, Func valueSelector, bool inherit = false) where TAttribute : Attribute { var att = type.GetMember(MemberName).FirstOrDefault() .GetCustomAttributes(typeof(TAttribute), inherit) .FirstOrDefault() as TAttribute; if (att != null) { return valueSelector(att); } return default; } } // 使用: class Program { static void Main() { // 讀取 MyClass 類的 MyMethod 方法的 Description 特性的值 var description = typeof(MyClass) .GetAttribute("MyMethod", (DescriptionAttribute d) => d.Description); Console.WriteLine(description); // 輸出:Hello } } public class MyClass { [Description("Hello")] public void MyMethod() { } } ``` ## 動態例項化介面的所有實現類(外掛啟用) 通過反射來動態例項化某個介面的所有實現類,常用於實現系統的外掛式開發。比如在程式啟動的時候去讀取指定資料夾(如 Plugins)中的 dll 檔案,通過反射獲取 dll 中所有實現了某個介面的類,並在適當的時候將其例項化。大致實現如下: ```csharp interface IPlugin { string Description { get; } void DoWork(); } ``` 某個在獨立 dll 中的類: ```csharp class HelloPlugin : IPlugin { public string Description => "A plugin that says Hello"; public void DoWork() { Console.WriteLine("Hello"); } } ``` 在你的系統啟動的時候動態載入該 dll,讀取實現了 IPlugin 介面的所有類的資訊,並將其例項化。 ```csharp public IEnumerable InstantiatePlugins(string directory) { var assemblyNames = Directory.GetFiles(directory, "*.addin.dll") .Select(name => new FileInfo(name).FullName).ToArray(); foreach (var fileName assemblyNames) AppDomain.CurrentDomain.Load(File.ReadAllBytes(fileName)); var assemblies = assemblyNames.Select(System.Reflection.Assembly.LoadFile); var typesInAssembly = assemblies.SelectMany(asm =>asm.GetTypes()); var pluginTypes = typesInAssembly.Where(type => typeof (IPlugin).IsAssignableFrom(type)); return pluginTypes.Select(Activator.CreateInstance).Cast(); } ``` ## 檢查泛型例項的泛型引數 前文提到了構造泛型和具象泛型,這裡解釋一下。大多時候我們所說的泛型都是指**構造泛型**,有時候也被稱為**具象泛型**。比如 `List` 就是一個構造泛型,因為它可以通過 new 來例項化。相應的,`List<>` 泛型是**非構造泛型**,有時候也被稱為**開放泛型**,它不能被例項化。開放泛型通過反射可以轉換為任意的具象泛型,這一點前文有示例。 假如現在有一個泛型例項,出於某種需求,我們想知道構建這個泛型例項需要用什麼泛型引數。比如某人建立了一個 `List` 泛型的例項,並把它作為引數傳給了我們的一個方法: ```csharp var myList = newList(); ShowGenericArguments(myList); ``` 我們的方法簽名是這樣的: ```csharp public void ShowGenericArguments(object o) ``` 這時,作為此方法的編寫者,我們並不知道這個 o 物件具體是用什麼型別的泛型引數構建的。通過反射,我們可以得到泛型例項的很多資訊,其中最簡單的就是判斷一個型別是不是泛型: ```csharp public void ShowGenericArguments(object o) { if (o == null) return; Type t =o.GetType(); if (!t.IsGenericType) return; ... } ``` 由於 `List<>` 本身也是泛型,所以上面的判斷不嚴謹,我們需要知道的是物件是不是一個構造泛型(`List`)。而 Type 類還提供了一些有用的屬性: ```csharp typeof(List<>).IsGenericType // true typeof(List<>).IsGenericTypeDefinition // true typeof(List<>).IsConstructedGenericType// false typeof(List).IsGenericType // true typeof(List).IsGenericTypeDefinition // false typeof(List).IsConstructedGenericType// true ``` `IsConstructedGenericType` 和 `IsGenericTypeDefinition` 分別用來判斷某個泛型是不是構造泛型和非構造泛型。 再結合 Type 的 `GetGenericArguments()` 方法,就可以很容易地知道某個泛型例項是用什麼泛型引數構建的了,例如: ```csharp static void ShowGenericArguments(object o) { if (o == null) return; Type t = o.GetType(); if (!t.IsConstructedGenericType) return; foreach (Type genericTypeArgument in t.GetGenericArguments()) Console.WriteLine(genericTypeArgument.Name); } ``` 以上是關於反射的乾貨知識,都是從實際專案開發中總結而來,希望對你的開發有幫助。