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
Examples
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". To run this code, paste it into an empty console project, and make sure to include a reference 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();
}
}
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 are 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 are 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 the .NET Framework 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 methods should not access the list of attributes of a reflected object or method directly by calling the GetCustomAttributes method on the provided MemberInfo or ParameterInfo instance, but should instead use the declaredAttributes
list, which is passed as a parameter to the GetCustomAttributes method overloads.
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 will 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 may 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.
Warning
A CustomReflectionContext object alters the attributes returned by a particular reflection object, such as those obtained by the GetCustomAttributes method. It does not alter the custom attribute data returned by the GetCustomAttributesData method, and these two lists will not match when you use a custom reflection context.
Constructors
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
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>) |
Creates an object that represents a property to be added to a type, to be used with the AddProperties(Type) method. |
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. |
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) |