Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Reflection and DynamicMethods

Reflection and DynamicMethods

While most .NET developers have heard of reflection, many have not yet implemented reflection in their applications to its fullest potential.  Essentially, reflection is the run-time examination of an object's members.  Zach Smith's reflection article on TechRepublic provides sample code and a few suggested uses for reflection:

Reflection can be used to implement a wide range of functionality. Most serialization engines use reflection to extract data from the objects that need to be serialized. Reflection can also be used to create customizable application architectures through the use of plug-in modules. Many Object Relation Modeling (ORM) solutions also take advantage of reflection to implement dynamic mapping functionality

A few other suggestions:  code generation, accessing private members of 3rd party objects(such as Microsoft controls), and dynamically generating unit tests based on an object's methods.  The possible uses of reflection are endless.  How many more can you come up with?

In addition, with .NET 2.0 provides a new DynamicMethod class in the System.Reflection.Emit namespace that provides even faster access to an object's properties or fields.  This class allows direct access to the Intermediate Language (IL) generated for the object, with much faster execution than traditional reflection techniques in .NET 1.x.  Read more about these techniques in Fast Dynamic Property/Field Accessors.

Rate this Article