[C#] Using Reflection

I explained what reflection means in a .Net world in the previous post “Reflection – Overview“. In this post, I will show you how you can reflect types, methods, and properties in your application.

1. Reflecting on Constructors

You can use the “GetConstructor()” method of the type reference.

public Object Invoke(Object[] parameters)

The “ConstructorInfo” provides an overloaded “Invoke” method to call the constructor.

Type sbType = typeof(System.Text.StringBuilder);

ConstructorInfo constructor = sbType.GetConstructor(Type.EmptyTypes);
object myObj = constructor.Invoke(null);

Console.WriteLine("{0}", myObj.GetType().FullName);

2. Reflecting on Methods

Once you get the reference to the type, you can get a list of methods using “GetMethods()” or get a specific method using the “GetMethod()”.

Each method is represented by the “System.Reflection.MethodInfo” object.

public virtual Type ReturnType { get; }
public abstract ParameterInfo[] GetParameters()

To reflect on parameters, use the “GetParameters()” method.

Type sbType = typeof(System.Text.StringBuilder);

ConstructorInfo constructor = sbType.GetConstructor(new Type[] { typeof(String) });
object myObj = constructor.Invoke(new object[] { "Hello" }); // StringBuilder object

MethodInfo append = sbType.GetMethod("Append", new Type[] { typeof(String) });
append.Invoke(myObj, new object[] { ", Wolrd!" });

MethodInfo toString = sbType.GetMethod("ToString", Type.EmptyTypes);
Console.WriteLine("{0}", toString.Invoke(myObj, null)); // Hello, World!

3. Reflecting on Properties

You can use “GetProperty(name)” and “GetProperties()” methods of the “System.Type” to reflect on properties.

The “PropertyInfo” lets you get or set a value.

public abstract PropertyAttributes Attributes { get; }
public abstract bool CanRead { get; }
public abstract bool CanWrite { get; }

public virtual Object GetValue(Object obj, Object[] index)
public virtual void SetValue(Object obj, Object value, Object[] index)

The “index” parameter is used for indexers and needs to set to null for general properties.

Type eType = typeof(System.Exception);

ConstructorInfo constructor = eType.GetConstructor(new Type[] { typeof(String) });
object myObj = constructor.Invoke(new object[] { "Custom Exception" }); // with a message

PropertyInfo helpLink = eType.GetProperty("HelpLink");
helpLink.SetValue(myObj, @"my.com/help", null);

PropertyInfo message = eType.GetProperty("Message");
Console.WriteLine("{0}: {1}", message.GetValue(myObj, null), helpLink.GetValue(myObj, null));

4. Reflecting on Implemented Interfaces

You can use “GetInterface(name)” and “GetInterfaces()” methods of the “System.Type” to reflect on interfaces supported on the referenced type.

Type aType = typeof(System.Reflection.Assembly);

foreach (Type t in aType.GetInterfaces())
{
  Console.WriteLine("{0}", t.FullName);
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s