Tuesday, March 15, 2016


You can get type information, read manifest or read metadata from an assembly using reflection programmatically. Late binding also can achieve using reflection. Using reflection application can get the information about itself. Method can invoke using reflection. You can get the information about type, properties, methods and event object. We can create object of a class or use existing object using reflection.
System.Reflection namespace along with System.Type used in getting information from an assembly. System.Type is an abstract class, its represent a type in Common Type System.
System.Type represent type declaration such as class type, array type, interface type, value type, enumeration type , open or close constructed generic type.

You can get the type object using two ways.
1.       Using GetType() method of Type class

Type t = Type.GetType("Reflection.Car", false, true);

Where Reflection is namespace and Car is a class name.
2.       Using typeof() operator.
 // Get the Type using typeof.
Type t = typeof(Car);


1.       Get the type of an object at runtime.
2.       Load an assembly at runtime and call meth\od dynamically at runtime. Basically useful for people who are working in an environment where configuration is changing at runtime.
3.       Get object information at runtime even call them
4.       Late binding
5.       Can create a class at runtime using Reflection.Emit
6.       Can create property and methods at runtime using PropertyBuilder and MethodBuilder.
7.       Activator.CreateInstance() method can be used to create object at runtime.
8.       Dynamic keyword introduced in .Net 4.0 uses reflection internally.

Practical Use of Reflection

1. Property window of visual studio is a live example of reflection where it reads the all properties of object and show them
2. In unit testing framework, you need to call method and properties dynamically.
3. Sometimes need to show all class information in a text file.

Difference Between Reflection and dynamic
1.       Using dynamic only public members can invoke while using reflection both can be invoke.
2.       Using dynamic only instance member can invoke while using reflection static members also cab invoke.
3.       Dynamic is a keyword introduce in .Net 4.0 while reflection is a mechanism to inspect the meta data of an object at runtime. Dynamic cannot inspect the metadata about an object.

Interview Questions related to Reflection

Que. What do you mean by late Binding?
Ans. When an object does not know which method should be called at compile time and it knows at runtime it is called late binding.

Que. What do you mean by custom binding?
Ans. When we load and assembly at runtime and get the type information at runtime , and then invoke the method you want it is called custom binding.

Que. Which class is most important in Reflection
Ans. System.Type.

Que. How do you get type object of an assembly without loading an assembly?
Ans. By using Assembly.GetTypes() or Assembly.GetType() method.

Que. How do you get the type object of an assembly if assembly is already loaded.
Ans. By using Type.GetType() method.

Que. How do you get Module type objects?
Ans. By using Module.GetTpes() and Module.GetType() method to get the type of Module.

Que. Which property is used to determine whether the type is generic?
Ans. By using IsGenericType property , or you can use IsGenericTypeDefinition to determine whether the type is a generic type definition.

Que. Which namespace contains the classed that used to build type at run time, means class, method or property generation at runtime.

No comments: