Reflecting On The Code Dom

1,163 views
1,064 views

Published on

Step by Step review of the interactions and parallels between Reflection and the CodeDom

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,163
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
20
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Reflecting On The Code Dom

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

×