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

閱讀本文大概需要 3 分鐘。

通常,反射用於動態獲取對象的類型、屬性和方法等信息。今天帶你玩轉反射,來匯總一下反射的各種常見操作,撿漏看看有沒有你不知道的。

獲取類型的成員

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,默認返回的是所有公開的成員。

獲取並調用對象的方法

Type 類型的 GetMethod 方法用來獲取該類型的 MethodInfo,然後可通過 MethodInfo 動態調用該方法。

對於非靜態方法,需要傳遞對應的實例作為參數,示例:

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
    }
}

對於靜態方法,則對象參數傳空,示例:

var method = typeof(Math).GetMethod("Exp");
// 相當於 Math.Exp(2)
var result = method.Invoke(null, new object[] {2});
Console.WriteLine(result); // 輸出(e^2):7.38905609893065

如果是泛型方法,則還需要通過泛型參數來創建泛型方法,示例:

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 條件聲明

如果在一個方法內需要動態創建一個實例,可以直接使用 new 條件聲明,例如:

T GetInstance<T>() where T : new()
{
    T instance = newT();
    return instance;
}

但這種方式適用場景有限,比如不適用於構造函數帶參數的類型。

使用 Activator 類

使用 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 類

如果你想創建某個類的實例的時候不執行構造函數和屬性初始化,可以使用 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

IsConstructedGenericTypeIsGenericTypeDefinition 分別用來判斷某個泛型是不是構造泛型和非構造泛型。

再結合 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);
}

以上是關於反射的乾貨知識,都是從實際項目開發中總結而來,希望對你的開發有幫助。

本站聲明:網站內容來源於博客園,如有侵權,請聯繫我們,我們將及時處理

【其他文章推薦】

※廣告預算用在刀口上,台北網頁設計公司幫您達到更多曝光效益

※別再煩惱如何寫文案,掌握八大原則!

※教你寫出一流的銷售文案?

※超省錢租車方案

※廣告預算用在刀口上,台北網頁設計公司幫您達到更多曝光效益

※產品缺大量曝光嗎?你需要的是一流包裝設計!

※回頭車貨運收費標準