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 "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 하위 클래스 지정 하 고 관련 메서드를 재정의 하 여 있습니다 수 추가, 제거 또는 반영된 매개 변수 또는 멤버에 적용 되는 특성을 변경 또는 새 속성을 추가 하지만 변경 하지 않고 단순히 래핑하고 리플렉션 개체를 리플 렉 트 된 형식입니다.

예를 들어, 코드 팩터리 메서드를 특정 특성을 적용 하는 규칙을 따르는지 하지만 특성이 없는 제 3 자 코드를 작성 하려면 이제 해야 합니다. 사용할 수 있습니다 CustomReflectionContext 특성이 있어야 하는 개체를 식별 하는 것에 대 한 규칙을 지정 하 고 코드에서 볼 때 이러한 특성이 포함 된 개체를 제공 합니다.

사용할 CustomReflectionContext 리플 렉 트 된 개체를 사용 하는 코드 개체는 연결 된 런타임 리플렉션 컨텍스트를 사용 하 여 반영 하는 모든 것으로 가정 하는 대신 리플렉션 컨텍스트를 지정 하는 개념을 지원 해야 합니다는 효과적으로 합니다. .NET Framework의 많은 리플렉션 메서드는 제공 된 ReflectionContext 이 목적을 위해 매개 변수입니다.

리플 렉 트 된 매개 변수 또는 멤버에 적용 되는 특성을 수정 하려면 합니다 GetCustomAttributes(ParameterInfo, IEnumerable<Object>) 또는 GetCustomAttributes(MemberInfo, IEnumerable<Object>) 메서드. 이러한 메서드는 반영된 개체 및 해당 현재 리플렉션 컨텍스트에서 특성의 목록 및 사용자 지정 리플렉션 컨텍스트의 있어야 하는 특성의 목록을 반환 합니다.

경고

CustomReflectionContext 메서드를 호출 하 여 직접 반영된 개체 또는 메서드 특성 목록에 액세스 하지 않아야 합니다 GetCustomAttributes 메서드를 제공 된 MemberInfo 또는 ParameterInfo 인스턴스를 대신 사용 해야 하지만 declaredAttributes 매개 변수로 전달 되는 목록 에 GetCustomAttributes 메서드 오버 로드 합니다.

리플 렉 트 된 형식으로 속성을 추가 하려면 재정의 AddProperties 메서드. 메서드는 반영된 유형을 지정 하 고 추가 속성의 목록을 반환 하는 매개 변수를 허용 합니다. 사용 해야는 CreateProperty 메서드를 반환 하는 속성 개체를 만듭니다. 속성 접근자를 될 하는 속성을 만들기 및 읽기 전용 또는 쓰기 전용 속성 접근자 중 하나를 생략할 수 있습니다 하는 경우 대리자를 지정할 수 있습니다. 이러한 더미 속성 메타 데이터 또는 중간 언어 (CIL (공용) 백업 없습니다 있는 note 합니다.

경고

때문일 반영된 개체 간의 같음에 대 한 주의 리플렉션 컨텍스트를 사용 하 여 작업할 때 개체가 여러 컨텍스트에서 동일한 리플 렉 트 된 개체를 나타낼 수 있습니다. 사용할 수는 MapType 메서드 리플 렉 트 된 개체의 특정 리플렉션 컨텍스트의 버전을 가져옵니다.

경고

A 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>)

지정된 사용자 지정 특성을 사용하여 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)

적용 대상