Click below for Step by Step and Interview Q & A (dotnetinter) wrote,
Click below for Step by Step and Interview Q & A
dotnetinter

.NET interview questions: - What is Reflection its implementation and practical uses?

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.
                                                              g1


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.

                                                            g2

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())
{Console.WriteLine(objMemberInfo.Name);}

// Browse through properties.
foreach (PropertyInfo objPropertyInfo in parameterType.GetProperties())
{Console.WriteLine(objPropertyInfo.Name);}


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: -

Tags: .net, .net interview questions, csharp interview questions, it interview questions with answers, programming iterview questions, reflection
Subscribe

  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments