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:



Figure A


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:


  • 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.


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


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


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:


Figure E

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


Figure F

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


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:


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.

Editor's Picks