Reflection Functionality in a .NET Framework Application

The meaning of reflection is to provide information on an assembly, type, a method, a constructor or an interface implemented. Every time you run the .NET Reflector, or simply use the dot syntax to explore the members of a class by Intellisense, you use reflection.

Reflection is particularly useful when you have no compile time knowledge of a given type (let’s say for example when you want to create an extendable application). To implement and load a snap-in, you’d create a method which checks for an assembly (the snap-in itself). You’d mark the classes with attributes, or even better, implement a predefined interface (which this time will really act as a contract). What you’d do is the following:

  1. Create a Class Library (e.g.: Commons.dll), and define your interface there.
  2. Create your main application, and set a reference on the Commons.dll
  3. Create a method which checks for the implemented contract interface in a given assembly (or in a group of assemblies, for example in the \AppPathSnapIns directory.
  4. Create some snap-ins, set a reference on the Commons.dll, and create classes which implements it.
  5. Run your application.


I think the only thing which needs explanation is the checking for the implemented interface in an assembly you haven’t added reference at compile time. This is exactly something which needs reflection. How would you do it?

Assembly assem = Assembly.LoadFrom(@”C:AppPathSnapInssnapin.dll”);
foreach(Type t in assem.GetTypes())
{
if(t.GetInterface(“ISnapIn”) != null)
Console.WriteLine(t.Name);
}

This method prints all classes which implements the ISnapIn interface. For example, you could create an instance of a type (using late-binding) instead of simply print out its name by creating an object instance, and the call:

Object o = Activator.CreateInstance(t);

If you’d like to call a method (with no parameters) you could use the following syntax:

MethodInfo mi = t.GetMethod(“SomeMethod”);
mi.Invoke(o, null);

If you’d got some parameters, you’d simply create an object array with them, and pass it to the Invoke method instead of just passing null.

As you could have seen, the main classes here are Type, Assembly and Activator. Type has a massive number of methods, the most interesting is GetType, which returns a type reference from a string. It has three constructor overloads:

  • String TypeName
  • String TypeName, Boolean ThrowError
  • String TypeName, Boolean ThrowError, Boolean IgnoreCase

When you’ve acquired a type reference, you could easily create an instance of it by using the static methods of Activator. Activator.CreateInstance is a such method. It returns an object, and needs a simple Type parameter, in its simplest form. You should understand that to be able to call instance methods, you’d need an instance (in this case, a late-bound one) of the class. You should pass this instance whenever you request a method invoke, need a property, etc.

The Assembly class has the ability to load an assembly in reflection-only or normal mode. Reflection-only mode means that you cannot call methods or create instances of classes, just examine them. When you need an assembly form the GAC, you should call Assembly.Load, when needing to load from a file path, use the Assembly.LoadFrom method.

There are a number of assembly attributes which you should be familiar with. They have very describing names, and you can meet them if you open your AssemblyInfo.cs file. They have the prefix of assembly:

An example:

[assembly: AssemblyTitle(“MyApllication”)]

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