一般,反射用於動態獲取對象的類型、屬性和方法等信息。今天帶你玩轉反射,來彙總一下反射的各類常見操做,撿漏看看有沒有你不知道的。bash
Type 類的 GetMembers 方法用來獲取該類型的全部成員,包括方法和屬性,可經過 BindingFlags 標誌來篩選這些成員。函數
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}"); } } }
輸出:性能
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,默認返回的是全部公開的成員。this
Type 類型的 GetMethod 方法用來獲取該類型的 MethodInfo,而後可經過 MethodInfo 動態調用該方法。spa
對於非靜態方法,須要傳遞對應的實例做爲參數,示例:插件
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 } }
對於靜態方法,則對象參數傳空,示例:code
var method = typeof(Math).GetMethod("Exp"); // 至關於 Math.Exp(2) var result = method.Invoke(null, new object[] {2}); Console.WriteLine(result); // 輸出(e^2):7.38905609893065
若是是泛型方法,則還須要經過泛型參數來建立泛型方法,示例:orm
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<T>() { //... } public static void StaticMethod<T>() { //... } }
使用反射動態建立一個類型的實例有多種種方式。最簡單的一種是用 new()
條件聲明。對象
若是在一個方法內須要動態建立一個實例,能夠直接使用 new 條件聲明,例如:接口
T GetInstance<T>() where T : new() { T instance = newT(); return instance; }
但這種方式適用場景有限,好比不適用於構造函數帶參數的類型。
使用 Activator 類動態建立一個類的實例是最多見的作法,示例:
Type type = typeof(BigInteger); object result = Activator.CreateInstance(type); Console.WriteLine(result); // 輸出:0 result = Activator.CreateInstance(type, 123); Console.WriteLine(result); // 輸出:123
動態建立泛類型實例,須要先建立開放泛型(如List<>
),再根據泛型參數轉換爲具象泛型(如List<string>
),示例:
// 先建立開放泛型 Type openType = typeof(List<>); // 再建立具象泛型 Type[] tArgs = { typeof(string) }; Type target = openType.MakeGenericType(tArgs); // 最後建立泛型實例 List<string> result = (List<string>)Activator.CreateInstance(target);
若是你不知道什麼是開放泛型和具象泛型,請看本文最後一節。
也能夠經過反射構造器的方式動態建立類的實例,比上面使用 Activator 類要稍稍麻煩些,但性能要好些。示例:
ConstructorInfo c = typeof(T).GetConstructor(new[] { typeof(string) }); if (c == null) throw new InvalidOperationException("..."); T instance = (T)c.Invoke(new object[] { "test" });
若是你想建立某個類的實例的時候不執行構造函數和屬性初始化,可使用 FormatterServices 的 GetUninitializedObject 方法。示例:
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 方法提取爲一個強類型委託:
var tArgs = new Type[] { typeof(int), typeof(int) }; var maxMethod = typeof(Math).GetMethod("Max", tArgs); var strongTypeDelegate = (Func<int, int, int>)Delegate .CreateDelegate(typeof(Func<int, int, int>), null, maxMethod); Console.WriteLine("3 和 5 之間最大的是:{0}", strongTypeDelegate(3, 5)); // 輸出:5
這個技巧也適用於屬性,能夠獲取強類型的 Getter 和 Setter。示例:
var theProperty = typeof(MyClass).GetProperty("MyIntProperty"); // 強類型 Getter var theGetter = theProperty.GetGetMethod(); var strongTypeGetter = (Func<MyClass, int>)Delegate .CreateDelegate(typeof(Func<MyClass, int>), theGetter); var intVal = strongTypeGetter(target); // 相關於:target.MyIntProperty // 強類型 Setter var theSetter = theProperty.GetSetMethod(); var strongTypeSetter = (Action<MyClass, int>)Delegate .CreateDelegate(typeof(Action<MyClass, int>), theSetter); strongTypeSetter(target, 5); // 至關於:target.MyIntProperty = 5
如下是四個常見的場景示例。
示例一,找出一個類中標註了某個自定義特性(好比 MyAtrribute)的屬性。
var props = type .GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance) .Where(prop =>Attribute.IsDefined(prop, typeof(MyAttribute)));
示例二,找出某個屬性的全部自定義特性。
var attributes = typeof(t).GetProperty("Name").GetCustomAttributes(false);
示例三,找出程序集全部標註了某個自定義特性的類。
static IEnumerable<Type> GetTypesWithAttribute(Assembly assembly) { foreach(Type type inassembly.GetTypes()) { if (type.GetCustomAttributes(typeof(MyAttribute), true).Length > 0) { yield return type; } } }
示例四,在運行時讀取自定義特性的值
public static class AttributeExtensions { public static TValue GetAttribute<TAttribute, TValue>( this Type type, string MemberName, Func<TAttribute, TValue> 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 中全部實現了某個接口的類,並在適當的時候將其實例化。大體實現以下:
interface IPlugin { string Description { get; } void DoWork(); }
某個在獨立 dll 中的類:
class HelloPlugin : IPlugin { public string Description => "A plugin that says Hello"; public void DoWork() { Console.WriteLine("Hello"); } }
在你的系統啓動的時候動態加載該 dll,讀取實現了 IPlugin 接口的全部類的信息,並將其實例化。
public IEnumerable<IPlugin> 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<IPlugin>(); }
前文提到了構造泛型和具象泛型,這裏解釋一下。大多時候咱們所說的泛型都是指構造泛型,有時候也被稱爲具象泛型。好比 List<int>
就是一個構造泛型,由於它能夠經過 new 來實例化。相應的,List<>
泛型是非構造泛型,有時候也被稱爲開放泛型,它不能被實例化。開放泛型經過反射能夠轉換爲任意的具象泛型,這一點前文有示例。
假如如今有一個泛型實例,出於某種需求,咱們想知道構建這個泛型實例須要用什麼泛型參數。好比某人建立了一個 List<T>
泛型的實例,並把它做爲參數傳給了咱們的一個方法:
var myList = newList<int>(); ShowGenericArguments(myList);
咱們的方法簽名是這樣的:
public void ShowGenericArguments(object o)
這時,做爲此方法的編寫者,咱們並不知道這個 o 對象具體是用什麼類型的泛型參數構建的。經過反射,咱們能夠獲得泛型實例的不少信息,其中最簡單的就是判斷一個類型是否是泛型:
public void ShowGenericArguments(object o) { if (o == null) return; Type t =o.GetType(); if (!t.IsGenericType) return; ... }
因爲 List<>
自己也是泛型,因此上面的判斷不嚴謹,咱們須要知道的是對象是否是一個構造泛型(List<int>
)。而 Type 類還提供了一些有用的屬性:
typeof(List<>).IsGenericType // true typeof(List<>).IsGenericTypeDefinition // true typeof(List<>).IsConstructedGenericType// false typeof(List<int>).IsGenericType // true typeof(List<int>).IsGenericTypeDefinition // false typeof(List<int>).IsConstructedGenericType// true
IsConstructedGenericType
和 IsGenericTypeDefinition
分別用來判斷某個泛型是否是構造泛型和非構造泛型。
再結合 Type 的 GetGenericArguments()
方法,就能夠很容易地知道某個泛型實例是用什麼泛型參數構建的了,例如:
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); }
以上是關於反射的乾貨知識,都是從實際項目開發中總結而來,但願對你的開發有幫助。