Using Reflection in C# – Part 2

Introduction

In part-1 of this series, we started our journey with reflection basics.

We learned that Reflection is the process by which a software can observe and modify its own structure and behavior.

For that to happen, we need helper objects to dynamically create instance of a type, bind the type to an existing object and getting the type from an existing object.

These helper objects are contained in System.Reflection namespace.

We also learned that Reflection is commonly used but it should be used with care as being relatively slow, error prone due to its dynamic nature and being complex in general.

If you are new to these topics, I’ll suggest to read the part-1 first as we will be building on top of previous learning.

Demo – Type Information using Reflection

Lets see the code for a vehicle entity:

We have a simple class Truck, which is extending a base class Vehicle.

All the source code is available from this git repository.

Following screenshot is showing the use of helper methods from reflection API to get details about Truck Type. Here we are listing all the methods implemented by Truck type:

(Note, it is not listing protected method UpdateSpeed(), we will come back to it when discussing BindingFlags later in post).

Reflection API is not limited to methods only, we can get constructors, events, fields, properties and all other kind of information. Following example shows the code to get the properties of a type:

GetProperties() returns and array of PropertyInfo type which contains information about the properties of the type.

Furthermore, we can see there are a wide range of helper methods available for use:

Also note that these helper methods are returning various different types of objects such as ConstructorInfo, MethodInfo, PropertyInfo and so on. Lets learn a little bit about these Info objects.

Info Objects

With these info objects, we have access to specialized objects for all type of members. It’s these objects that essentially describe methods, constructors, fields and so on.

MemberInfo

MemberInfo in C# is a very basic object when working with reflection. It is an abstract class that contains functionality and behavior, including metadata associated with all kind of members and other specialized info classes are derived from it.

MethodInfo

This drives from MemberInfo and it adds access to method-related metadata.

FieldInfo

This drives from MemberInfo and adds field-related metadata.

PropertyInfo

Also drives from MemberInfo and add property-related metadata.

EventInfo:

You may have already guessed it, it adds event related metadata.

and so on…. .These classes are the heart of reflection.

BindingFlags Enumeration

  • It is used to control bindings and to control how reflection searches for members and types.
  • It controls the binding itself
    • BindingFlags.GetProperty()
    • BindingFlags.SetField
  • Can be combined bitwise.

Lets see the following code snippets:

with these flags Instance, Public we are saying, that we are not interested in static or private properties. DeclaredOnly dictates not to lookup base types thus we are not getting Id and LicensePlate properties from base Vehicle Type.

Similar to that, if we wanted to show Private members, we can use these binding flags. Following example shows listing of protected member UpdateSpeed with use of BindingFlag:

Demo – Obtaining Property Values of an Instance

Let’s see the Vehicle domain in code:

and following is the ReportHelper

and here is the calling code:

Requirement

We want our software to display all the properties of a vehicle object and it shall also support any new vehicle type.

Now, if we try to use typical static approach, it is going to be very difficult, because there could be many different type of vehicles, some will be added even in future, so there is no easy way that our software can know this information at compile time:

As we can see in above shown code, that at compile-time, we have access to only properties from Base class Vehicle.

Solution

We can use Reflection to obtain property values of an instance at run-time.

Here is the updated method code:

following is the code from ReflectionHelper:

First thing you notice that method accept an object type instead of vehicle type because the logic is ok for any type.

As you can see that we used GetProperties() method with BindingFlags to get array of PropertyInfo type (PropertyInfo describes a property).

PropertyInfo is a compile time construct and it has a Name property which returns the name of the property. But the value depnds on instance. So to get the value, we invoke a method PropertyInfo.GetValue() passing it the actual instance that we are interested in.

here is the calling code and result:

As you can see that our code is able to pull this information which is not available until run-time.

The real power of this approach, using reflection, is that you can easily plugin new types. check the following code:

You can see that without making any changes to ReportHelper.WriteV2() method, it automatically picked up & displayed the correct properties of another type of vehicle. There’s no easy way that we could achieve that without using reflection.

With this example, we’ll finish this part and lets continue our learning in upcoming posts.

Summary

In this post, we continue our journey about learning C# reflection. We learn how to get type object and how to gain other information about methods and members. Gathering metadata, is the first step in almost all reflection processes.

We also learned about various useful classes such as MemerbInfo, MethodInfo, PropertyInfo, TypeInfo etc offered by Reflection API.

Demos source code is available on this git repository.

Let me know if you have some questions or comments. Till next time, Happy Coding.