this blog contains information for .net and sql stuffs. You can find various tips and tricks to overcome problem you may be facing in ...

Monday, April 26, 2010

Reflection in .net (Reflection Part-1)

As a .net developer or any time worked with .net we are familiar with word Reflection. We rarely use reflection in our development but it is very powerful feature provided by .net framework.

It gives information of types, methods, properties, code etc at runtime dynamically generated assembly. With use of reflection we can access assembly written in .net or any other platform or even we can generate or create assembly at runtime as per our need.

To use reflection we need to import or add System.Reflection and or System.Reflection.Emit namespace in our application.

Let’s take a look how to use reflection to create type instance at run time.

We want to show string length in console and for that we don’t want to use Length property.

To use any type at dynamically we should follow below steps.

àCreate instance of type

Type t = typeof(StringBuilder);

àCreate constructor of type

ConstructorInfo ci = t.GetConstructor(new Type[] { typeof(string) });

àInvoke constructor

Object sb = ci.Invoke(new Object[] { "Hello, world!" });

àCreate a PropertyInfo instance representing the StringBuilder.Length property.

PropertyInfo lengthProperty = t.GetProperty("Length");

àRetrieve the Length property and cast it to the native type.

int len = (int)lengthProperty.GetValue(sb, null);


We can use any type as per above example it’s not necessary that it must be provided by .net framework. we can even create instance of type created by ourselves dll files.

If we check assembly’s information in project property we can see assembly’s name, assembly’s version, assembly’s copy write information, assembly’s version and many things that gives information for an assembly we can access all these information with use of Reflection.

First we need to load assembly in our current application domain.

That can be done by methods provided by Assembly class all are static methods.

Assembly.Load :Loads an assembly by name, usually from the Global Assembly

Cache (GAC)

Assembly.LoadFile :Loads an assembly by specifying the filename

Assembly.LoadFrom :Loads an assembly given its filename or path

Following methods allows to load assembly but only for examine we don’t run code of that assembly. That is also code reflection-only context.

Assembly.ReflectionOnlyLoad :Loads an assembly in reflection-only context, usually from the GAC

Assembly.ReflectionOnlyLoadFrom :Loads an assembly in reflection-only context, by specifying the filename

We can access assembly’s information by its attribute first need to iterate through attributes of it.

Like below are some of the assembly’s attribute.




To access all members like property, methods ,fields members we can call GetMembers. In that we can pass different BindingFlags enumerator.

Depends of BindingFlags we get array of members in a given assembly. We can also pass more than one BindingFlags enumerator.

Type t = typeof(String);

MemberInfo[] mi = t.GetMembers(BindingFlags.NonPublic | BindingFlags.Static);

Above code will return all the private and static members of String type. There are also other options in BindingFlags enumerator depends on it we get list of members.

In next part we will discuss about how to create assembly at runtime.

No comments: