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 提供了一种从反射对象添加或删除自定义属性的方法,或者向这些对象添加虚拟属性,而无需重新实现完整的反射模型。 默认值 CustomReflectionContext 只需包装反射对象而不进行任何更改,但通过子类化和重写相关方法,可以添加、删除或更改应用于任何反射参数或成员的属性,或向反射类型添加新属性。

例如,假设代码遵循将特定属性应用于工厂方法的约定,但现在需要使用缺少属性的第三方代码。 可用于 CustomReflectionContext 指定一个规则,用于标识应具有属性的对象,并在从代码中查看这些属性时向这些对象提供这些属性。

若要有效使用 CustomReflectionContext ,使用反射对象的代码必须支持指定反射上下文的概念,而不是假定所有反射对象都与运行时反射上下文相关联。 .NET中的许多反射方法都为此提供了参数ReflectionContext

若要修改应用于反射参数或成员的属性,请重写 GetCustomAttributes(ParameterInfo, IEnumerable<Object>)GetCustomAttributes(MemberInfo, IEnumerable<Object>) 方法。 这些方法采用反射对象及其当前反射上下文下的属性列表,并返回它在自定义反射上下文下应具有的属性列表。

Warning

CustomReflectionContext重写不应查询提供的或MemberInfo通过调用ParameterInfo的属性GetCustomAttributes;请改用declaredAttributes传递给重载的GetCustomAttributes序列。

若要将属性添加到反射类型,请重写该方法 AddProperties。 该方法接受指定反射类型的参数,并返回其他属性的列表。 应使用 CreateProperty 该方法创建要返回的属性对象。 可以在创建充当属性访问器的属性时指定委托,并且可以省略其中一个访问器来创建只读或仅写属性。 请注意,此类虚拟属性没有元数据或公共中间语言(CIL)支持。

Warning

  • 处理反射上下文时,请谨慎处理反射对象之间的相等性,因为对象可能在多个上下文中表示相同的反射对象。 可以使用该方法 MapType 获取特定反射上下文的反射对象版本。
  • 某个 CustomReflectionContext 对象会更改特定反射对象所返回的属性,例如通过 GetCustomAttributes 方法获取的属性。 它不会更改GetCustomAttributesData方法返回的自定义属性数据,这两个列表在使用自定义反射上下文时将不匹配。

以下示例演示如何对 CustomReflectionContext 进行子类化,以将自定义属性添加到名称以“To”开头的给定类型的所有成员。

// 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());
}

构造函数

名称 说明
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)

适用于