關于C#反射 你需要知道的
通常,反射用于動態(tài)獲取對象的類型、屬性和方法等信息。今天帶你玩轉(zhuǎn)反射,來匯總一下反射的各種常見操作,撿漏看看有沒有你不知道的。
獲取類型的成員
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,默認返回的是所有公開的成員。
獲取并調(diào)用對象的方法
Type 類型的 GetMethod 方法用來獲取該類型的 MethodInfo,然后可通過 MethodInfo 動態(tài)調(diào)用該方法。
對于非靜態(tài)方法,需要傳遞對應的實例作為參數(shù),示例:
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
}
}
對于靜態(tài)方法,則對象參數(shù)傳空,示例:
var method = typeof(Math).GetMethod("Exp");
// 相當于 Math.Exp(2)
var result = method.Invoke(null, new object[] {2});
Console.WriteLine(result); // 輸出(e^2):7.38905609893065
如果是泛型方法,則還需要通過泛型參數(shù)來創(chuàng)建泛型方法,示例:
class Program
{
public static void Main()
{
// 反射調(diào)用泛型方法
MethodInfo method1 = typeof(Sample).GetMethod("GenericMethod");
MethodInfo generic1 = method1.MakeGenericMethod(typeof(string));
generic1.Invoke(sample, null);
// 反射調(diào)用靜態(tài)泛型方法
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>()
{
//...
}
}
創(chuàng)建一個類型的實例
使用反射動態(tài)創(chuàng)建一個類型的實例有多種種方式。最簡單的一種是用 new() 條件聲明。
使用 new 條件聲明
如果在一個方法內(nèi)需要動態(tài)創(chuàng)建一個實例,可以直接使用 new 條件聲明,例如:
T GetInstance<T>() where T : new()
{
T instance = newT();
return instance;
}
但這種方式適用場景有限,比如不適用于構(gòu)造函數(shù)帶參數(shù)的類型。
使用 Activator 類
使用 Activator 類動態(tài)創(chuàng)建一個類的實例是最常見的做法,示例:
Type type = typeof(BigInteger); object result = Activator.CreateInstance(type); Console.WriteLine(result); // 輸出:0 result = Activator.CreateInstance(type, 123); Console.WriteLine(result); // 輸出:123
動態(tài)創(chuàng)建泛類型實例,需要先創(chuàng)建開放泛型(如List<>),再根據(jù)泛型參數(shù)轉(zhuǎn)換為具象泛型(如List<string>),示例:
// 先創(chuàng)建開放泛型
Type openType = typeof(List<>);
// 再創(chuàng)建具象泛型
Type[] tArgs = { typeof(string) };
Type target = openType.MakeGenericType(tArgs);
// 最后創(chuàng)建泛型實例
List<string> result = (List<string>)Activator.CreateInstance(target);
如果你不知道什么是開放泛型和具象泛型,請看本文最后一節(jié)。
使用構(gòu)造器反射
也可以通過反射構(gòu)造器的方式動態(tài)創(chuàng)建類的實例,比上面使用 Activator 類要稍稍麻煩些,但性能要好些。示例:
ConstructorInfo c = typeof(T).GetConstructor(new[] { typeof(string) });
if (c == null)
throw new InvalidOperationException("...");
T instance = (T)c.Invoke(new object[] { "test" });
使用 FormatterServices 類
如果你想創(chuàng)建某個類的實例的時候不執(zhí)行構(gòu)造函數(shù)和屬性初始化,可以使用 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;
}
獲取屬性或方法的強類型委托
通過反射獲取到對象的屬性和方法后,如果你想通過強類型的方法來訪問或調(diào)用,可以在中間加一層委托。這樣的好處是有利于封裝,調(diào)用者可以明確的知道調(diào)用時需要傳什么參數(shù)。 比如下面這個方法,把 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() { }
}
動態(tài)實例化接口的所有實現(xiàn)類(插件激活)
通過反射來動態(tài)實例化某個接口的所有實現(xiàn)類,常用于實現(xiàn)系統(tǒng)的插件式開發(fā)。比如在程序啟動的時候去讀取指定文件夾(如 Plugins)中的 dll 文件,通過反射獲取 dll 中所有實現(xiàn)了某個接口的類,并在適當?shù)臅r候?qū)⑵鋵嵗4笾聦崿F(xiàn)如下:
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");
}
}
在你的系統(tǒng)啟動的時候動態(tài)加載該 dll,讀取實現(xiàn)了 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>();
}
檢查泛型實例的泛型參數(shù)
前文提到了構(gòu)造泛型和具象泛型,這里解釋一下。大多時候我們所說的泛型都是指構(gòu)造泛型,有時候也被稱為具象泛型。比如 List<int> 就是一個構(gòu)造泛型,因為它可以通過 new 來實例化。相應的,List<> 泛型是非構(gòu)造泛型,有時候也被稱為開放泛型,它不能被實例化。開放泛型通過反射可以轉(zhuǎn)換為任意的具象泛型,這一點前文有示例。
假如現(xiàn)在有一個泛型實例,出于某種需求,我們想知道構(gòu)建這個泛型實例需要用什么泛型參數(shù)。比如某人創(chuàng)建了一個 List<T> 泛型的實例,并把它作為參數(shù)傳給了我們的一個方法:
var myList = newList<int>(); ShowGenericArguments(myList);
我們的方法簽名是這樣的:
public void ShowGenericArguments(object o)
這時,作為此方法的編寫者,我們并不知道這個 o 對象具體是用什么類型的泛型參數(shù)構(gòu)建的。通過反射,我們可以得到泛型實例的很多信息,其中最簡單的就是判斷一個類型是不是泛型:
public void ShowGenericArguments(object o)
{
if (o == null) return;
Type t =o.GetType();
if (!t.IsGenericType) return;
...
}
由于 List<> 本身也是泛型,所以上面的判斷不嚴謹,我們需要知道的是對象是不是一個構(gòu)造泛型(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 分別用來判斷某個泛型是不是構(gòu)造泛型和非構(gòu)造泛型。
再結(jié)合 Type 的 GetGenericArguments() 方法,就可以很容易地知道某個泛型實例是用什么泛型參數(shù)構(gòu)建的了,例如:
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);
}
以上就是深入了解C#之反射的詳細內(nèi)容,更多關于c# 反射的資料請關注腳本之家其它相關文章!

