Wednesday, March 24, 2010

What is Reflection

Reflection means to find out metadata information of any modules or assembly. You can also create class using reflection.

Developer can load any assembly runtime and find out metadata information of that assembly. He can also create object of any type which are exist in that assembly and also able to assign value and call any method from that object at runtime.

Let’s take an example, you have to use any third party control (DLL). You can get your requested type dynamically.

Here I explained you how to load any assembly which is added as a reference in your project.

You are getting one Test Library1 (TestLibrary1.dll) and you have to find out “Class1” type. You have to add reference of that class library and call this code snippet to load library:

Assembly assembly = Assembly.Load("TestLibrary1");
Type t = assembly.GetType("TestLibrary1.Class1");

Here we have pass “TestLibrary1” as a parameter of Assembly.Load method is an assembly name. This is same as a namespace name.

Assembly is in-built .net class which has static method “Load” which load assembly so developer can easily find out types.

After loading assembly, you can find out your type using GetType method of assembly object.

Load method of Assembly class has different overloads. You can also pass physical path or Assembly name or byte array of assembly name.

You can download code from here.

No comments:

Post a Comment

DotNet Code Guru