Log in

Previous Entry | Next Entry

Reflection is needed when you want to determine/inspect contents of an assembly. For example look at your visual studio editor intellisense, when you type “.” (dot) before any object, it gives you all members of the object. This is possible because of reflection.

Reflection also goes one step further; it can also invoke a member which is inspected.  For instance if the reflection detects that there is a method called as “GetChanges” in an object. We can get a reference to that method instance and invoke the same on runtime.
In simple words reflection passes through two steps “Inspect” and “Invoke” (optional). “Invoke” process is optional.


Implementation of reflection

Implementing reflection in c# is a two step process ,  1st get the “type” of the object and then use the type to browse members like “methods” , “properties” etc.

Step 1: - The first step is to get the type of the object. So for example you have a DLL called as “ClassLibrary1.dll” which has a class called as “Class1”.  We can use the “Assembly” (belongs to  System.Reflection namespace) class to get a reference to the type of the object. Later  we can use “Activator.CreateInstance” to create an instance of the class. The “GetType()” function helps us to get reference to the type of the object.

var myAssembly = Assembly.LoadFile(@"C:\ClassLibrary1.dll");
var myType = myAssembly.GetType("ClassLibrary1.Class1");
dynamic objMyClass = Activator.CreateInstance(myType);
// Get the class type
Type parameterType = objMyClass.GetType();

Step 2: - Once we have reference the type of the object we can then call “GetMembers” or “GetProperties” to browse throw the methods and properties of the class.

// Browse through members
foreach (MemberInfo objMemberInfo in parameterType.GetMembers())

// Browse through properties.
foreach (PropertyInfo objPropertyInfo in parameterType.GetProperties())

In case you want to invoke the member which you have inspected you can use “InvokeMember” to invoke the method. Below is the code for the same.

parameterType.InvokeMember("Display",BindingFlags.Public |
BindingFlags.NonPublic | BindingFlags.InvokeMethod |
BindingFlags.Instance,null, objMyClass, null);

Practical uses of reflection

  • If you are creating application like visual studio editors where you want show internal of an object by using intellisense.

  • In unit testing sometimes we need to invoke private methods. That’s a different thing test private members are proper or not.

  • Sometimes we would like to dump properties, methods and assembly references to a file or probably show it on a screen.

See the following .NET interview questions video on difference between Throw and Throw ex: -

Copy right www.questpond.com

Buy 600+ Real Time Interview Videos

Call +91-22-66752917 or mail us on questpond@questpond.com for more details on C#, .NET, ASP.NET, SQL Server interview question videos.

SQL Server interview question videos

Latest Month

January 2017


Powered by LiveJournal.com