Reflection in .Net Part - 1
Thursday, September 25th, 2008Reflection in .Net Part - 1
Reflection is a very interesting and powerful feature in .Net. Use reflection wisely and you can avoid dependencies. Today I did some really good reflection stuff and thought of writing this post.
You can dynamically load assemblies at runtime. You can instantiate Types, execute methods etc… When you instantiate classes from an external assembly, you need to know the full name of the assembly. In this first part of a series of posts on reflection, let us see scratch the surface of reflection.
System.Reflection is the most important namespace that we would be using in reflection.
In today’s post let us see how you can load a dll from a file, find the types that are in it.
using System.Reflection;
…
Assembly myAssembly = Assembly.LoadFile(”C:\\Example.dll”);
This LoadFile method loads the specified assembly into the caller’s domain.
Now we can list all the Type’s in the given assembly from the Assembly instance we just created.
Type[] allTypes = myAssembly.GetTypes();
The GetTypes() method returns an array of all the Types in the assembly.
There are several other interesting methods in the Assembly class. I will be covering the rest of the static methods of Assembly class in the next part.
References:
Assembly class on MSDN
Reflection in .Net Part - 1









