Upload
nick-harrison
View
973
Download
1
Embed Size (px)
DESCRIPTION
Step by Step review of the interactions and parallels between Reflection and the CodeDom
Citation preview
Reflecting on the CodeDom
What is Reflection?
Allows you to view metadata about classes Allows you to dynamically discover new
classes at runtime Allows you to dynamically invoke methods or
properties discovered at runtime Allows you to create new types at runtime
Why should we care about Reflection?
Viewing metadata allows us to automate documentation
Dynamically discovering classes make it easier to create plug-ins
Dynamically invoking new methods allows us to create adaptive solutions not dependant on recompiling
Creating new types at runtime allows us to convert scripting customizations to compiled code
A Gentle Tour of Reflection
Key data types– System.Type– System.Reflection.Assembly– System.Reflection.MemberInfo
System.Reflection.MethodInfo System.Reflection.ConstructorInfo System.Reflection.ProperyInfo System.Reflection.EventInfo
The Great and Mighty Type Object
Every Object provides a GetType method which will return a Type Object
Key Properties include:– Assembly will return the Assembly object where
this type is located– BaseType will return the Type object that this
Type is directly derived– IsClass, IsEnum, IsInterface will provide details
about the type of class
Type Object (Contd)
Key Methods include:– GetMethods will return an array of MethodInfo objects one
for each method defined in the Type– GetProperties will return an array of PropertyInfo objects
one for each property defined in the Type– GetEvents will return an array of EventInfo objects one for
each event defined in the Type– GetConstructors will return an array of ConstructorInfo
objects one for each event defined in the Type
Assembly Object
Provides metadata information about a DLL Key methods
– GetTypes will return an array of Type objects one for each type included in the assembly
Key static methods– Load will return the assembly specified– LoadFrom will return the assembly specified by
filename
MemberInfo
Base Class for MethodInfo, ConstructorInfo, ProperyInfo, EventInfo
Provides limited baseline common attributes such as:– Name– MemberType
Sample Code (Object Browser)
Select a DLL Detect all of the Classes Detect all of the Properties Detect all of the Methods Detect all of the Parameters for each Method
Commenting Code
Type Custom Attributes Apply Custom Attributes to your code Use Reflection to retrieve the custom
attributes from the Types in an Assembly
Defining a Custom Attribute
AttributeUsageAttribute– Define where an attribute can be used– Specify whether or not the attribute can be applied more
than once– Specify whether or not the attribute is inheritable
Derives from System.Attribute Define a Constructor to be called when an attribute is
added to code Define properties to expose attribute specific details
Applying Attributes to Code
C#[FlowerBoxAttribute ("Nick Harrison", “July 7,
2004", "A simple class to do some simple calcs")]
VB.Net<FlowerBoxAttribute("Nick Harrison", “July 7,
2004", "A simple class to do some simple calcs")>
Retrieving Attributes from Code
FlowerBoxAttribute [] FlowerBoxes;
FlowerBoxes = (FlowerBoxAttribute []) Member.GetCustomAttributes (typeof (FlowerBoxAttribute ), false);
foreach (CustomAttributes.FlowerBoxAttribute
FlowerBox in FlowerBoxes)
{
// Access the properties of the FlowerBoxAttribute
}
Creating a Plug-in
Store the location of the Assembly in a config file
Store the name of the Class in a config file Load the Assembly Instantiate the Type Convert the new type to a pre-defined
interface. Use the new interface object
CodeDom Demystified