Software Development

Applied Reflection: Dynamically accessing properties of a class at runtime

As one of the core features of reflection, the PropertyInfo object allows C# developers to create highly dynamic and reusable program modules. When used correctly, this functionality can help reduce development time and cost. Zach Smith explains how to use this important C# feature of reflection, and shows how it can be implemented in a real-world scenario.

Before you jump into using reflection in your business applications, there are a few basic concepts you need to understand, starting with how to access the properties of a class dynamically at runtime. If you've never used reflection, this may seem like an odd idea at first. You may even wonder what in the world you would ever use that type of functionality for. However, once you understand how this functionality works and start using it, you'll find that it’s not only easy to use, but it has uses in almost every business application.

While some developers may be tempted to take the seemingly easy way out and just hard-code program modules, in the end you can save yourself time and effort by making these modules dynamic and reusable. Microsoft has given developers a very easy way to accomplish this task in some situations via the PropertyInfo object.

Obtaining a PropertyInfo object

You’ll use the PropertyInfo class when you need to access class properties at runtime. A PropertyInfo instance will represent one property in the class (or Type) that you are examining. The PropertyInfo object describes the property and also acts as a proxy when you want to either get or set a property’s value.

Two of the most common methods of obtaining a PropertyInfo object are System.Type.GetProperty and System.Type.GetProperties. The GetProperty method returns one PropertyInfo object, while GetProperties returns an array of PropertyInfo objects. Here are two basic examples of these two methods:

System.Type.GetProperty

C#:

Figure A
System.Type.GetProperties

C#:

Figure B

Using the PropertyInfo object

Once you have a PropertyInfo object, you can use it to find out information about the property that it references. The following list shows the common methods and properties that you can access on the PropertyInfo object:

Properties:

  • CanRead – Returns true if this property contains an accessible GET accessor.
  • CanWrite – Returns true if this property contains an accessible SET accessor.
  • Name – Returns the name of the property.
  • PropertyType – Returns the Type that the property accepts and returns.

Methods:

  • GetValue – Used to retrieve the value of the property (CanRead must be true).

Example:

Figure C
  • SetValue – Used to set the value of the property (CanWrite must be true).

Example:

Figure D
  • GetCustomAttributes – Used to retrieve an array of custom attributes that have been applied to the property.

Using the properties and methods listed above, you can gather enough information about the property to access and modify it in any way you need to.

Business Uses

The ability to dynamically access class properties can significantly decrease the development time required for a project. For example, say we need to map one object’s properties into another object. Let’s assume that these are third-party components, and we cannot modify their source code. We’ll call the first object "Package" and define it with this code:

C#:

Figure E

We’ll name the other object "Shipment" and define it with this code:

C#:

Figure F

Using normal C# methods, you could use this function to map the properties of the Package class to the Shipment class:

C#:

Figure G

While this code looks simple, you can imagine how tedious it would be to write if there were more than a couple of properties in each class. Also, this method is not dynamic, so you would need to create a new method for each time you want to map different classes.

A different approach would be to use reflection in conjunction with an XML mapping file. This type of solution would allow you to dynamically map any type of class to any other type of class. The reflection portion of this solution is shown below:

C#:

Figure H

As you can see, while the reflection solution is more complicated, it is much more dynamic and reusable.

Modify class properties

In this article I explained how to use reflection to access and modify the properties of a class at runtime. The idea of modifying class properties at runtime is a building block on the road to using reflection to build dynamic and reusable code. In future articles, I will use the functions presented here to demonstrate additional real-world uses of reflection.

6 comments
Tony Hopkinson
Tony Hopkinson

of user configurability to the mapping, which I personally would think three or four times about. Use attributes instead. You get the flexibility you want without having to cart the file about. Better still the the classes / properties will all be in your face, so they'll be easier to keep in step.

skip.munk
skip.munk

You just saved me a lot of headaches. I really appreciate clear and to the point posts with relavant examples. Great job!

codezilla94
codezilla94

nice, here is a similar concept that creates dynamic object properties at runtime using reflection. http://www.spikesolutions.net/ViewSolution.aspx?ID=080da5ff-9c72-4760-9612-ac5448180a65 The solution comes with a user control for defining dynamic properties (DataType, Formatting, Validations) that render on an object at runtime. It also comes with a user control for setting dynamic properties on on an instance of an object based on the definitions. Various Web controls along with validation controls are rendered based on the definitions.

codezilla94
codezilla94

Here is a framework that creates dynamic properties at runtime. http://www.spikesolutions.net/ViewSolution.aspx?ID=080da5ff-9c72-4760-9612-ac5448180a65 The solution comes with a user control for defining dynamic properties (DataType, Formatting, Validations) that render on an object at runtime. It also comes with a user control for setting dynamic properties on on an instance of an object based on the definitions. Various Web controls along with validation controls are rendered based on the definitions.

ajay_kopperla
ajay_kopperla

Hi , this is the exact article which iam looking for . in my project i need similar one where i need to loop through the properties of the class.