CustomReflectionContext 類別

定義

表示可自訂的反射內容。Represents a customizable reflection context.

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 將自訂屬性加入至名稱開頭為 "to" 的指定型別之所有成員的子類別。The following example demonstrates how to subclass CustomReflectionContext to add a custom attribute to all the members of a given type whose names begin with "To". 若要執行此程式碼,請將它貼到空白的主控台專案中,並確定包含 System.Reflection.Context.dll 的參考。To run this code, paste it into an empty console project, and make sure to include a reference to System.Reflection.Context.dll.

//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 provides a way for you to add or remove custom attributes from reflection objects, or add dummy properties to those objects, without re-implementing the complete reflection model. 預設 CustomReflectionContext 只會包裝反映物件而不進行任何變更,但藉由子類別化和覆寫相關的方法,您可以加入、移除或變更套用至任何反映之參數或成員的屬性,或將新的屬性加入至反映的型別。The default CustomReflectionContext simply wraps reflection objects without making any changes, but by subclassing and overriding the relevant methods, you can add, remove, or change the attributes that apply to any reflected parameter or member, or add new properties to a reflected type.

例如,假設您的程式碼遵循將特定屬性套用至 factory 方法的慣例,但現在您必須使用缺少屬性的協力廠商程式碼。For example, suppose that your code follows the convention of applying a particular attribute to factory methods, but you are now required to work with third-party code that lacks attributes. 您可以使用 CustomReflectionContext 來指定規則,以識別應該具有屬性的物件,並在從您的程式碼中查看物件時提供這些屬性。You can use CustomReflectionContext to specify a rule for identifying the objects that should have attributes and to supply the objects with those attributes when they are viewed from your code.

若要 CustomReflectionContext 有效率地使用,使用反映物件的程式碼必須支援指定反映內容的概念,而不是假設所有反映的物件都與執行時間反映內容相關聯。To use CustomReflectionContext effectively, the code that uses the reflected objects must support the notion of specifying a reflection context, instead of assuming that all reflected objects are associated with the runtime reflection context. .NET Framework 中的許多反映方法都會提供 ReflectionContext 此用途的參數。Many reflection methods in the .NET Framework provide a ReflectionContext parameter for this purpose.

若要修改套用至反映之參數或成員的屬性,請覆寫 GetCustomAttributes(ParameterInfo, IEnumerable<Object>)GetCustomAttributes(MemberInfo, IEnumerable<Object>) 方法。To modify the attributes that are applied to a reflected parameter or member, override the GetCustomAttributes(ParameterInfo, IEnumerable<Object>) or GetCustomAttributes(MemberInfo, IEnumerable<Object>) method. 這些方法會取得反映的物件和其目前反映內容下的屬性清單,並傳回它在自訂反映內容下應該具有的屬性清單。These methods take the reflected object and the list of attributes under its current reflection context, and return the list of attributes it should have under the custom reflection context.

警告

CustomReflectionContext 方法不應直接藉由在提供的或實例上呼叫方法,來存取反映物件或方法的屬性清單 GetCustomAttributes MemberInfo ParameterInfo ,但應改用以 declaredAttributes 參數形式傳遞給 GetCustomAttributes 方法多載的清單。CustomReflectionContext methods should not access the list of attributes of a reflected object or method directly by calling the GetCustomAttributes method on the provided MemberInfo or ParameterInfo instance, but should instead use the declaredAttributes list, which is passed as a parameter to the GetCustomAttributes method overloads.

若要將屬性加入至反映的類型,請覆寫 AddProperties 方法。To add properties to a reflected type, override the AddProperties method. 方法會接受指定反映型別的參數,並傳回其他屬性清單。The method accepts a parameter that specifies the reflected type, and returns a list of additional properties. 您應該使用 CreateProperty 方法來建立要傳回的屬性物件。You should use the CreateProperty method to create property objects to return. 您可以在建立將做為屬性存取子的屬性時指定委派,也可以省略其中一個存取子,以建立唯讀或僅限寫入屬性。You can specify delegates when creating the property that will serve as the property accessor, and you can omit one of the accessors to create a read-only or write-only property. 請注意,這類虛擬屬性沒有中繼資料或通用中繼語言 (的 CIL) 支援。Note that such dummy properties have no metadata or Common Intermediate Language (CIL) backing.

警告

當您使用反映內容時,請留意反映物件之間的相等,因為物件在多個內容中可能代表相同的反映物件。Be cautious about equality among reflected objects when you work with reflection contexts, because objects may represent the same reflected object in multiple contexts. 您可以使用 MapType 方法來取得反映物件的特定反映內容版本。You can use the MapType method to obtain a particular reflection context's version of a reflected object.

警告

CustomReflectionContext物件會改變特定反映物件所傳回的屬性,例如方法所取得的屬性 GetCustomAttributesA CustomReflectionContext object alters the attributes returned by a particular reflection object, such as those obtained by the GetCustomAttributes method. 它不會改變方法所傳回的自訂屬性資料 GetCustomAttributesData ,而且當您使用自訂反映內容時,這兩個清單將不會相符。It does not alter the custom attribute data returned by the GetCustomAttributesData method, and these two lists will not match when you use a custom reflection context.

建構函式

CustomReflectionContext()

初始化 CustomReflectionContext 類別的新執行個體。Initializes a new instance of the CustomReflectionContext class.

CustomReflectionContext(ReflectionContext)

使用指定的反映內容為基底,初始化 CustomReflectionContext 類別的新執行個體。Initializes a new instance of the CustomReflectionContext class with the specified reflection context as a base.

方法

AddProperties(Type)

在衍生類別中覆寫時,為指定的類型提供額外屬性集合,如這個反射內容所表示。When overridden in a derived class, provides a collection of additional properties for the specified type, as represented in this reflection context.

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

建立表示要加入至型別之屬性的物件,該型別會搭配 AddProperties(Type) 方法使用。Creates an object that represents a property to be added to a type, to be used with the AddProperties(Type) method.

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

建立表示要加入至型別之屬性 (Property) 的物件,該型別會搭配 AddProperties(Type) 方法使用且使用指定的自訂屬性 (Attribute)。Creates an object that represents a property to be added to a type, to be used with the AddProperties(Type) method and using the specified custom attributes.

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

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

在衍生類別中覆寫時,為指定的成員提供自訂屬性清單,如這個反射內容所表示。When overridden in a derived class, provides a list of custom attributes for the specified member, as represented in this reflection context.

GetCustomAttributes(ParameterInfo, IEnumerable<Object>)

在衍生類別中覆寫時,為指定的參數提供自訂屬性的清單,如這個反射內容所表示。When overridden in a derived class, provides a list of custom attributes for the specified parameter, as represented in this reflection context.

GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
GetTypeForObject(Object)

取得此反映內容中指定之物件的型別表示。Gets the representation of the type of the specified object in this reflection context.

(繼承來源 ReflectionContext)
MapAssembly(Assembly)

取得此反射內容中由來自另一個反射內容的物件所代表的表示。Gets the representation, in this reflection context, of an assembly that is represented by an object from another reflection context.

MapType(TypeInfo)

取得此反射內容中由來自另一個反射內容的物件所代表的型別表示。Gets the representation, in this reflection context, of a type represented by an object from another reflection context.

MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

適用於