First question is why to use reflection, is there any scenario in which you need reflection ?
Okey let me try to explain from my previous project.
Scenario:-
I was working on a project; sometime back, and client needed one generic application; that could handle different messages from MSMQ (Microsoft Messaging Queue). Based on message type; totally different functionality needed to be executed. Further they did not want to touch one part when there was change in other part. Each functionality needed to be implemented in different assembly (or i would say dll in layman term)
There were many possible solutions; but the solution with which we came up finally; was use of reflection.
A single MSMQ Manager (application/windows service) was looking for new messages in MSQM, with each new message in queue, a new thread was created wwhich was calling service class(es) on the basis of configured data for that message type (all using reflection)
Each message type data was configured in Db-Table/XML-File; So while deploying new message types, there was no need to stop the current application.
Code: (In my favourite C#)
Lets come to real part, i.e. how to create and use objects dynamically:
(1) To create objects dynamically, first we need to load assembly
(2) Create constructor info object, and from that create instance of class
(3) Call function/sub-routine of class.
Note : We need System.Reflection namespace in our code.
//Load Assembly:-
Assembly asm = Assembly.Load(@"C:\CODE4CODER.COM\myAssembly.dll");
Type targetClassType = asm.GetType("FullyQualifiedNameOfClass"); /*Fully qualified name of class like Code4Coder.com.myClass */
Type[] argTypes = Type.EmptyTypes;
ConstructorInfo cInfo = targetClassType.GetConstructor(argTypes);
//Create instance
Object objTargetClass = cInfo.Invoke(new object(), argTypes);
//Call any method of object:
//==>Say method name is getCode4CoderMethod, no parameters in function, and it returns String:
MethodInfo mInfo = targetClassType.GetMethod("getCode4CoderMethod"); //Method Name
string s = Convert.ToString( mInfo.Invoke(objTargetClass, argTypes)); /*First parameter is object for which this method is called, and second parameter is "array of parameters passed to the method"*/
Comments/suggestions are welcomed.Assembly asm = Assembly.Load(@"C:\CODE4CODER.COM\myAssembly.dll");
Type targetClassType = asm.GetType("FullyQualifiedNameOfClass"); /*Fully qualified name of class like Code4Coder.com.myClass */
Type[] argTypes = Type.EmptyTypes;
ConstructorInfo cInfo = targetClassType.GetConstructor(argTypes);
//Create instance
Object objTargetClass = cInfo.Invoke(new object(), argTypes);
//Call any method of object:
//==>Say method name is getCode4CoderMethod, no parameters in function, and it returns String:
MethodInfo mInfo = targetClassType.GetMethod("getCode4CoderMethod"); //Method Name
string s = Convert.ToString( mInfo.Invoke(objTargetClass, argTypes)); /*First parameter is object for which this method is called, and second parameter is "array of parameters passed to the method"*/
I want to create object dynamically based on Type. and i need to copy the data to that Object.Any body Help me Plz...........
ReplyDelete