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 (&quot;Nick Harrison&quot;, “July 7, 2004&quot;, &quot;A simple class to do some simple calcs&quot;)]   VB.Net <FlowerBoxAttribute(&quot;Nick Harrison&quot;, “July 7, 2004&quot;, &quot;A simple class to do some simple calcs&quot;)>
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

Reflecting On The Code Dom

  • 1.
  • 2.
    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
  • 3.
    Why should wecare 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
  • 4.
    A Gentle Tourof Reflection Key data types System.Type System.Reflection.Assembly System.Reflection.MemberInfo System.Reflection.MethodInfo System.Reflection.ConstructorInfo System.Reflection.ProperyInfo System.Reflection.EventInfo
  • 5.
    The Great andMighty 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
  • 6.
    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
  • 7.
    Assembly Object Providesmetadata 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
  • 8.
    MemberInfo Base Classfor MethodInfo, ConstructorInfo, ProperyInfo, EventInfo Provides limited baseline common attributes such as: Name MemberType
  • 9.
    Sample Code (ObjectBrowser) 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
  • 10.
    Commenting Code TypeCustom Attributes Apply Custom Attributes to your code Use Reflection to retrieve the custom attributes from the Types in an Assembly
  • 11.
    Defining a CustomAttribute 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
  • 12.
    Applying Attributes toCode C# [FlowerBoxAttribute (&quot;Nick Harrison&quot;, “July 7, 2004&quot;, &quot;A simple class to do some simple calcs&quot;)] VB.Net <FlowerBoxAttribute(&quot;Nick Harrison&quot;, “July 7, 2004&quot;, &quot;A simple class to do some simple calcs&quot;)>
  • 13.
    Retrieving Attributes fromCode FlowerBoxAttribute [] FlowerBoxes; FlowerBoxes = (FlowerBoxAttribute []) Member.GetCustomAttributes (typeof (FlowerBoxAttribute ), false); foreach (CustomAttributes.FlowerBoxAttribute FlowerBox in FlowerBoxes) { // Access the properties of the FlowerBoxAttribute }
  • 14.
    Creating a Plug-inStore 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
  • 15.