Share via


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 只是包装反射对象而不进行任何更改,但通过子类化和重写相关方法,可以添加、删除或更改应用于任何反射参数或成员的属性,或者向反射类型添加新属性。

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

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

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

警告

CustomReflectionContext方法不应通过在提供的 MemberInfo 或 实例上调用 GetCustomAttributes 方法直接访问反射对象或ParameterInfo方法的属性列表,而应改用 declaredAttributes 列表,该列表作为参数传递给GetCustomAttributes方法重载。

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

警告

使用反射上下文时,请注意反射对象之间的相等性,因为对象可能在多个上下文中表示同一个反射对象。 可以使用 MapType 方法获取反射对象的特定反射上下文版本。

警告

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)

适用于