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
派生

次の例では、名前が "To" で始まる特定の型のすべてのメンバーにカスタム属性を追加する CustomReflectionContext をサブクラス化する方法を示します。 このコードを実行するには、空のコンソール プロジェクトに貼り付け、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 は、変更を加えずにリフレクション オブジェクトをラップするだけですが、関連するメソッドをサブクラス化してオーバーライドすることで、リフレクションされたパラメーターまたはメンバーに適用される属性を追加、削除、または変更したり、リフレクションされた型に新しいプロパティを追加したりできます。

たとえば、コードがファクトリ メソッドに特定の属性を適用する規則に従っているが、属性がないサードパーティのコードを操作する必要があるとします。 CustomReflectionContextを使用すると、属性を持つ必要があるオブジェクトを識別するためのルールを指定し、コードからそれらの属性を表示するときにそれらのオブジェクトを指定できます。

CustomReflectionContextを効果的に使用するには、リフレクション オブジェクトを使用するコードは、すべてのリフレクション オブジェクトがランタイム リフレクション コンテキストに関連付けられていると想定するのではなく、リフレクション コンテキストを指定するという概念をサポートする必要があります。 .NET Framework の多くのリフレクション メソッドは、この目的のために ReflectionContext パラメーターを提供します。

反映されたパラメーターまたはメンバーに適用される属性を変更するには、 GetCustomAttributes(ParameterInfo, IEnumerable<Object>) メソッドまたは GetCustomAttributes(MemberInfo, IEnumerable<Object>) メソッドをオーバーライドします。 これらのメソッドは、現在のリフレクション コンテキストでリフレクション オブジェクトと属性の一覧を取得し、カスタム リフレクション コンテキストの下で持つ必要がある属性の一覧を返します。

Warning

CustomReflectionContextメソッドは、指定されたGetCustomAttributesまたはMemberInfo インスタンスでParameterInfo メソッドを呼び出して、反映されたオブジェクトまたはメソッドの属性の一覧に直接アクセスするのではなく、declaredAttributes メソッドのオーバーロードにパラメーターとして渡されるGetCustomAttributes リストを使用する必要があります。

反映された型にプロパティを追加するには、 AddProperties メソッドをオーバーライドします。 このメソッドは、反映された型を指定するパラメーターを受け取り、追加のプロパティの一覧を返します。 CreateProperty メソッドを使用して、返すプロパティ オブジェクトを作成する必要があります。 プロパティ アクセサーとして機能するプロパティを作成するときにデリゲートを指定できます。また、いずれかのアクセサーを省略して、読み取り専用または書き込み専用のプロパティを作成できます。 このようなダミー プロパティには、メタデータや共通中間言語 (CIL) バッキングがないことに注意してください。

Warning

リフレクション コンテキストを使用する場合は、オブジェクトが複数のコンテキストで同じリフレクション オブジェクトを表す可能性があるため、リフレクション オブジェクト間の等価性に注意してください。 MapType メソッドを使用して、特定のリフレクション コンテキストのバージョンのリフレクション オブジェクトを取得できます。

Warning

CustomReflectionContext オブジェクトは、特定のリフレクション オブジェクトによって返される属性 (GetCustomAttributes メソッドによって取得されたものなど) を変更します。 GetCustomAttributesData メソッドによって返されるカスタム属性データは変更されず、カスタム リフレクション コンテキストを使用する場合、これら 2 つのリストは一致しません。

コンストラクター

名前 説明
CustomReflectionContext()

CustomReflectionContext クラスの新しいインスタンスを初期化します。

CustomReflectionContext(ReflectionContext)

指定したリフレクション コンテキストをベースとして、 CustomReflectionContext クラスの新しいインスタンスを初期化します。

メソッド

名前 説明
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)

適用対象