閱讀英文版本

分享方式:


CustomReflectionContext 類別

定義

表示可自訂的反射內容。

C#
public abstract class CustomReflectionContext : System.Reflection.ReflectionContext
繼承
CustomReflectionContext
衍生

範例

下列範例示範如何將 CustomReflectionContext 自定義屬性新增至名稱開頭為 「To」 之指定型別的所有成員。 若要執行此程式碼,請將它貼到空的主控台專案中,並確定包含 System.Reflection.Context.dll 的參考。

C#
//A blank example attribute.
class myAttribute : Attribute
{
}

//Reflection context with custom rules.
class myCRC : 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")) {
                   yield return new myAttribute();
               }
               //Keep existing attributes as well.
               foreach (var attr in declaredAttributes) yield return attr;
         }
}

class Program
{
    static void Main(string[] args)
    {
        myCRC mc = new myCRC();
        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 all the members of the type and their attributes.
        foreach (MemberInfo m in myTI.DeclaredMembers)
        {
           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();

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

        Console.WriteLine();

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

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

        Console.ReadLine();
    }
}

備註

CustomReflectionContext 可讓您從反映物件新增或移除自定義屬性,或將虛擬屬性新增至這些物件,而不需重新實作完整的反映模型。 默認 CustomReflectionContext 只會包裝反映物件而不進行任何變更,但藉由子類別化和覆寫相關的方法,您可以新增、移除或變更套用至任何反映參數或成員的屬性,或將新屬性新增至反映的類型。

例如,假設您的程式代碼遵循將特定屬性套用至 Factory 方法的慣例,但您現在必須使用缺少屬性的第三方程式代碼。 您可以使用 CustomReflectionContext 來指定規則來識別應該具有屬性的物件,以及在從程式代碼檢視這些屬性時提供這些屬性的物件。

若要有效使用 CustomReflectionContext ,使用反映物件的程式代碼必須支援指定反映內容的概念,而不是假設所有反映的物件都與運行時間反映內容相關聯。 .NET Framework 中的許多反映方法會為此目的提供ReflectionContext參數。

若要修改套用至反映的參數或成員的屬性,請覆寫 GetCustomAttributes(ParameterInfo, IEnumerable<Object>)GetCustomAttributes(MemberInfo, IEnumerable<Object>) 方法。 這些方法會採用反映的物件及其目前反映內容下的屬性清單,並傳回它應該在自定義反映內容下的屬性清單。

警告

CustomReflectionContext方法不應該直接透過在提供的 MemberInfo 或 實例上呼叫 GetCustomAttributes 方法,來存取反映之物件或ParameterInfo方法的屬性清單,而是應該改用declaredAttributes清單,該清單會當做參數傳遞至GetCustomAttributes方法多載。

若要將屬性新增至反映的類型,請覆寫 AddProperties 方法。 方法會接受指定反映類型的參數,並傳回其他屬性的清單。 您應該使用 CreateProperty 方法來建立要傳回的屬性物件。 您可以在建立將做為屬性存取子的屬性時指定委派,而且您可以省略其中一個存取子來建立唯讀或只讀屬性。 請注意,這類虛擬屬性沒有元數據或通用中繼語言 (CIL) 備份。

警告

當您使用反映內容時,請小心處理反映對象之間的相等,因為物件可能會在多個內容中代表相同的反映物件。 您可以使用 MapType 方法來取得特定反映內容的反映物件版本。

警告

CustomReflectionContext物件會改變特定反映物件所傳回的屬性,例如方法取得的屬性GetCustomAttributes。 它不會改變 方法所傳回的 GetCustomAttributesData 自定義屬性數據,而且當您使用自定義反映內容時,這兩個清單將不會相符。

建構函式

CustomReflectionContext()

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

CustomReflectionContext(ReflectionContext)

使用指定的反映內容為基底,初始化 CustomReflectionContext 類別的新執行個體。

方法

AddProperties(Type)

在衍生類別中覆寫時,為指定的類型提供額外屬性集合,如這個反射內容所表示。

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

建立表示要加入至型別之屬性的物件,該型別會搭配 AddProperties(Type) 方法使用。

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

建立表示要加入至型別之屬性 (Property) 的物件,該型別會搭配 AddProperties(Type) 方法使用且使用指定的自訂屬性 (Attribute)。

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)

適用於

產品 版本
.NET Core 1.0, Core 1.1, 6, 7, 8, 9
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0