CustomReflectionContext 類別

定義

代表可自訂的反射情境。

public ref class CustomReflectionContext abstract : System::Reflection::ReflectionContext
public abstract class CustomReflectionContext : System.Reflection.ReflectionContext
type CustomReflectionContext = class
    inherit ReflectionContext
Public MustInherit Class CustomReflectionContext
Inherits ReflectionContext
繼承
CustomReflectionContext
衍生

備註

CustomReflectionContext 提供一種方式,讓你能從反射物件中新增或移除自訂屬性,或在這些物件上加入虛擬屬性,而無需重新實作完整的反射模型。 預設 CustomReflectionContext 方式只是將反射物件包裹,無需做任何修改,但透過子分類並覆寫相關方法,你可以新增、移除或更改適用於任何反射參數或成員的屬性,或為反射型態新增屬性。

舉例來說,假設你的程式碼遵循將特定屬性套用到工廠方法的慣例,但你現在必須使用沒有屬性的第三方程式碼。 你可以用 CustomReflectionContext 來指定規則來識別應該有屬性的物件,並在從程式碼中看到物件時提供這些屬性。

為了有效使用 CustomReflectionContext ,使用反射物件的程式碼必須支援指定反射上下文的概念,而非假設所有反射物件都與執行時反射上下文相關聯。 許多 .NET 中的反射方法都提供ReflectionContext此目的的參數。

若要修改應用於反射參數或成員的屬性,請覆寫 GetCustomAttributes(ParameterInfo, IEnumerable<Object>)GetCustomAttributes(MemberInfo, IEnumerable<Object>) 方法。 這些方法會取得被反射物件及其當前反射上下文下的屬性清單,並回傳它在自訂反射上下文下應有的屬性清單。

Warning

CustomReflectionContext 覆寫不應查詢提供的 MemberInfo 屬性或 ParameterInfo 呼叫 GetCustomAttributes;改用 declaredAttributes 傳遞給超載的 GetCustomAttributes 序列。

若要為反射型別新增屬性,請覆寫該 AddProperties 方法。 該方法接受一個指定反射型別的參數,並回傳一串額外的屬性。 你應該用這個 CreateProperty 方法來建立並回傳屬性物件。 你可以在建立屬性時指定代理,以作為屬性訪問器,並且可以省略其中一個訪問器,以建立唯讀或只寫的屬性。 請注意,這類虛擬屬性沒有元資料或通用中介語言(CIL)支持。

Warning

  • 在處理反射情境時,要注意被反射物體之間的相等性,因為物體在多個情境中可能代表同一個反射物體。 你可以用這個 MapType 方法取得特定反射情境中反射物體的版本。
  • 物件 CustomReflectionContext 會改變特定反射物件所回傳的屬性,例如該 GetCustomAttributes 方法所獲得的屬性。 它不會改變方法回傳 GetCustomAttributesData 的自訂屬性資料,而且當你使用自訂反射上下文時,這兩個清單也不會匹配。

以下範例示範如何子類 CustomReflectionContext ,為所有以「To」開頭的給定類型成員新增自訂屬性。

// A blank example attribute.
class MyAttribute : Attribute
{
}

// Reflection context with custom rules.
class MyCustomReflectionContext : CustomReflectionContext
{
    // Called whenever the reflection context checks for custom attributes.
    protected override IEnumerable<object> GetCustomAttributes(MemberInfo member, IEnumerable<object> declaredAttributes)
    {
        // Add example attribute to "To*" members.
        if (member.Name.StartsWith("To", StringComparison.Ordinal))
        {
            yield return new MyAttribute();
        }
        // Keep existing attributes as well.
        foreach (object attr in declaredAttributes)
        {
            yield return attr;
        }
    }
}
MyCustomReflectionContext mc = new();
Type t = typeof(string);

// A representation of the type in the default reflection context.
TypeInfo ti = t.GetTypeInfo();

// A representation of the type in the customized reflection context.
TypeInfo myTI = mc.MapType(ti);

// Display affected members of the type ("To*") and their attributes.
foreach (MemberInfo m in myTI.DeclaredMembers.Where(static m => m.Name.StartsWith("To", StringComparison.Ordinal)))
{
    Console.WriteLine($"{m.Name}:");
    foreach (Attribute cd in m.GetCustomAttributes())
    {
        Console.WriteLine(cd.GetType());
    }
}

Console.WriteLine();

// The "ToString" member as represented in the default reflection context.
MemberInfo mi1 = ti.GetDeclaredMethods("ToString").FirstOrDefault();

Attribute[] defaultAttributes = mi1.GetCustomAttributes().ToArray();

// All the attributes of "ToString" in the default reflection context.
Console.WriteLine("'ToString' Attributes in Default Reflection Context:");
foreach (Attribute cd in defaultAttributes)
{
    Console.WriteLine(cd.GetType());
}

Console.WriteLine();

// The same member in the custom reflection context.
mi1 = myTI.GetDeclaredMethods("ToString").FirstOrDefault();

Attribute[] customAttributes = mi1.GetCustomAttributes().ToArray();

// All its attributes, for comparison. MyAttribute is now included.
Console.WriteLine("'ToString' Attributes in Custom Reflection Context:");
foreach (Attribute cd in customAttributes)
{
    Console.WriteLine(cd.GetType());
}

建構函式

名稱 Description
CustomReflectionContext()

初始化 CustomReflectionContext 類別的新執行個體。

CustomReflectionContext(ReflectionContext)

以指定的反射上下文作為基底,初始化一個新的類別實例 CustomReflectionContext

方法

名稱 Description
AddProperties(Type)

當在派生類別中覆寫時,會為指定型別提供一組額外的屬性,如本反射語境所示。

CreateProperty(Type, String, Func<Object,Object>, Action<Object,Object>, IEnumerable<Attribute>, IEnumerable<Attribute>, IEnumerable<Attribute>)

建立一個物件,代表一個屬性,需加入到某個型別中,並 AddProperties(Type) 搭配該方法並使用指定的自訂屬性。

CreateProperty(Type, String, Func<Object,Object>, Action<Object,Object>)

建立一個物件,代表一個屬性,要加入到某個型別中,並搭配該 AddProperties(Type) 方法一起使用。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetCustomAttributes(MemberInfo, IEnumerable<Object>)

當在衍生類別中覆寫時,會提供指定成員的自訂屬性清單,如本反射上下文所示。

GetCustomAttributes(ParameterInfo, IEnumerable<Object>)

當在衍生類別中覆寫時,會提供指定參數的自訂屬性清單,如同此反思上下文所示。

GetHashCode()

做為預設哈希函式。

(繼承來源 Object)
GetType()

取得目前實例的 Type

(繼承來源 Object)
GetTypeForObject(Object)

取得該物體在此反射上下文中類型表示。

(繼承來源 ReflectionContext)
MapAssembly(Assembly)

在此反射語境中,得到由另一個反射語境中物件所表示的組裝體的表示。

MapType(TypeInfo)

在此反射語境中,得到由另一個反射語境中物件所表示的類型表示。

MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)

適用於