15
Reflecting on the CodeDom

Reflecting On The Code Dom

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: Reflecting On The Code Dom

Reflecting on the CodeDom

Page 2: Reflecting On The Code Dom

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

Page 3: Reflecting On The Code Dom

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

Page 4: Reflecting On The Code Dom

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

Page 5: Reflecting On The Code Dom

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

Page 6: Reflecting On The Code Dom

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

Page 7: Reflecting On The Code Dom

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

Page 8: Reflecting On The Code Dom

MemberInfo

Base Class for MethodInfo, ConstructorInfo, ProperyInfo, EventInfo

Provides limited baseline common attributes such as:– Name– MemberType

Page 9: Reflecting On The Code Dom

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

Page 10: Reflecting On The Code Dom

Commenting Code

Type Custom Attributes Apply Custom Attributes to your code Use Reflection to retrieve the custom

attributes from the Types in an Assembly

Page 11: Reflecting On The Code Dom

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

Page 12: Reflecting On The Code Dom

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")>

Page 13: Reflecting On The Code Dom

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

}

Page 14: Reflecting On The Code Dom

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

Page 15: Reflecting On The Code Dom

CodeDom Demystified