CustomReflectionContext 클래스
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
사용자 지정 가능한 리플렉션 컨텍스트를 나타냅니다.
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 하려면 반사된 개체를 사용하는 코드는 반사된 모든 개체가 런타임 리플렉션 컨텍스트와 연결되어 있다고 가정하는 대신 리플렉션 컨텍스트를 지정하는 개념을 지원해야 합니다. .NET 많은 리플렉션 메서드는 이 목적을 위한 매개 변수를 ReflectionContext 제공합니다.
반영된 매개 변수 또는 멤버에 적용되는 특성을 수정하려면 GetCustomAttributes(ParameterInfo, IEnumerable<Object>) 메서드나 GetCustomAttributes(MemberInfo, IEnumerable<Object>) 메서드를 재정의하십시오. 이러한 메서드는 현재 리플렉션 컨텍스트에서 반사된 개체와 특성 목록을 가져와서 사용자 지정 리플렉션 컨텍스트 아래에 있어야 하는 특성 목록을 반환합니다.
Warning
CustomReflectionContext재정의는 제공되거나 MemberInfo 호출하여 특성을 쿼리해서는 안 됩니다. 대신 오버로드에 ParameterInfo 전달된 시퀀스를 사용합니다GetCustomAttributes.declaredAttributesGetCustomAttributes
반영된 형식에 속성을 추가하려면 AddProperties 메서드를 재정의합니다. 이 메서드는 반사된 형식을 지정하는 매개 변수를 허용하고 추가 속성 목록을 반환합니다. 속성 개체를 반환하려면 CreateProperty 메서드를 사용해야 합니다. 속성 접근자 역할을 하는 속성을 만들 때 대리자를 지정할 수 있으며 접근자 중 하나를 생략하여 읽기 전용 또는 쓰기 전용 속성을 만들 수 있습니다. 이러한 더미 속성에는 메타데이터 또는 CIL(공용 중간 언어) 백업이 없습니다.
Warning
- 반사 컨텍스트를 사용할 때는 개체가 복수의 컨텍스트에서 동일한 반사 개체를 나타낼 수 있으므로 반사된 개체 간의 동일성에 주의를 기울여야 합니다. 이 메서드를 MapType 사용하여 특정 리플렉션 컨텍스트의 리플렉션된 개체 버전을 가져올 수 있습니다.
- CustomReflectionContext 객체는 GetCustomAttributes 메서드를 통해 얻은 특성과 같은 특정 리플렉션 객체에서 반환된 특성을 수정합니다. 메서드에서 반환 GetCustomAttributesData 된 사용자 지정 특성 데이터는 변경되지 않으며 사용자 지정 리플렉션 컨텍스트를 사용하는 경우 이러한 두 목록이 일치하지 않습니다.
다음 예제에서는 이름이 "To"로 시작하는 지정된 형식의 모든 멤버에 사용자 지정 특성을 추가하도록 서브클래스 CustomReflectionContext 하는 방법을 보여 줍니다.
// 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());
}
생성자
| Name | Description |
|---|---|
| CustomReflectionContext() |
CustomReflectionContext 클래스의 새 인스턴스를 초기화합니다. |
| CustomReflectionContext(ReflectionContext) |
지정된 리플렉션 컨텍스트를 CustomReflectionContext 기본으로 사용하여 클래스의 새 인스턴스를 초기화합니다. |
메서드
| Name | 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) |