CustomReflectionContext Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Represents a customizable reflection context.
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
- Inheritance
- Derived
Remarks
CustomReflectionContext provides a way for you to add or remove custom attributes from reflection objects, or add dummy properties to those objects, without re-implementing the complete reflection model. The default CustomReflectionContext simply wraps reflection objects without making any changes, but by subclassing and overriding the relevant methods, you can add, remove, or change the attributes that apply to any reflected parameter or member, or add new properties to a reflected type.
For example, suppose that your code follows the convention of applying a particular attribute to factory methods, but you're now required to work with third-party code that lacks attributes. You can use CustomReflectionContext to specify a rule for identifying the objects that should have attributes and to supply the objects with those attributes when they're viewed from your code.
To use CustomReflectionContext effectively, the code that uses the reflected objects must support the notion of specifying a reflection context, instead of assuming that all reflected objects are associated with the runtime reflection context. Many reflection methods in .NET provide a ReflectionContext parameter for this purpose.
To modify the attributes that are applied to a reflected parameter or member, override the GetCustomAttributes(ParameterInfo, IEnumerable<Object>) or GetCustomAttributes(MemberInfo, IEnumerable<Object>) method. These methods take the reflected object and the list of attributes under its current reflection context, and return the list of attributes it should have under the custom reflection context.
Warning
CustomReflectionContext overrides should not query attributes on the provided MemberInfo or ParameterInfo
by calling GetCustomAttributes; use the declaredAttributes sequence passed to the GetCustomAttributes overloads instead.
To add properties to a reflected type, override the AddProperties method. The method accepts a parameter that specifies the reflected type, and returns a list of additional properties. You should use the CreateProperty method to create property objects to return. You can specify delegates when creating the property that serve as the property accessor, and you can omit one of the accessors to create a read-only or write-only property. Note that such dummy properties have no metadata or Common Intermediate Language (CIL) backing.
Warning
- Be cautious about equality among reflected objects when you work with reflection contexts, because objects might represent the same reflected object in multiple contexts. You can use the MapType method to obtain a particular reflection context's version of a reflected object.
- A CustomReflectionContext object alters the attributes returned by a particular reflection object, such as those obtained by the GetCustomAttributes method. It doesn't alter the custom attribute data returned by the GetCustomAttributesData method, and these two lists won't match when you use a custom reflection context.
The following example demonstrates how to subclass CustomReflectionContext to add a custom attribute to all the members of a given type whose names begin with "To".
Constructors
| Name | Description |
|---|---|
| CustomReflectionContext() |
Initializes a new instance of the CustomReflectionContext class. |
| CustomReflectionContext(ReflectionContext) |
Initializes a new instance of the CustomReflectionContext class with the specified reflection context as a base. |
Methods
| Name | Description |
|---|---|
| AddProperties(Type) |
When overridden in a derived class, provides a collection of additional properties for the specified type, as represented in this reflection context. |
| CreateProperty(Type, String, Func<Object,Object>, Action<Object,Object>, IEnumerable<Attribute>, IEnumerable<Attribute>, IEnumerable<Attribute>) |
Creates an object that represents a property to be added to a type, to be used with the AddProperties(Type) method and using the specified custom attributes. |
| CreateProperty(Type, String, Func<Object,Object>, Action<Object,Object>) |
Creates an object that represents a property to be added to a type, to be used with the AddProperties(Type) method. |
| Equals(Object) |
Determines whether the specified object is equal to the current object. (Inherited from Object) |
| GetCustomAttributes(MemberInfo, IEnumerable<Object>) |
When overridden in a derived class, provides a list of custom attributes for the specified member, as represented in this reflection context. |
| GetCustomAttributes(ParameterInfo, IEnumerable<Object>) |
When overridden in a derived class, provides a list of custom attributes for the specified parameter, as represented in this reflection context. |
| GetHashCode() |
Serves as the default hash function. (Inherited from Object) |
| GetType() |
Gets the Type of the current instance. (Inherited from Object) |
| GetTypeForObject(Object) |
Gets the representation of the type of the specified object in this reflection context. (Inherited from ReflectionContext) |
| MapAssembly(Assembly) |
Gets the representation, in this reflection context, of an assembly that is represented by an object from another reflection context. |
| MapType(TypeInfo) |
Gets the representation, in this reflection context, of a type represented by an object from another reflection context. |
| MemberwiseClone() |
Creates a shallow copy of the current Object. (Inherited from Object) |
| ToString() |
Returns a string that represents the current object. (Inherited from Object) |