Saturday, November 13, 2010

C# :- Dynamic Keyword and Reflection

With help Dynamic keyword in C# we can invoke methods on the types without specifying the types at compile time . Once the Dynamic keyword is used compiler do not try to infer the type from the context  as  var keyword do , but instead rely upon the runtime to resolve the call . I found Dynamic keyword very useful while using reflection . I got in the scenario were I have the types name specified in the xml file and I have to create the instance of these types using reflection.  Lets say for instance i have types  with names  type1 and type2  in a xml file,  having method RoutineOne()  in both classes  and there implementation in a  .dll ( implementation.dll ) .  To create the instances using reflection I have to do following :--

         Type refelectedType;
         Assembly refelectedAssembly = null;

refelectedAssembly = Assembly.LoadFrom(dllPathName);
refelectedType = refelectedAssembly.GetType(“type1”);

       type1 routine = (type1)Activator.CreateInstance(refelectedType);
        routine.RoutineOne();

and do same thing to get the instance of “type2”.  We can see that we have to typecast the instance into respective  type. Or we can create an Interface and get common method in this interface. Lets say interface  ICommonRoutines :-

public interface ICommonRoutines
    {
        void RoutineOne();

    }

After implementing the interface in both the classes we can again do :-

         Type refelectedType;
         Assembly refelectedAssembly = null;

refelectedAssembly = Assembly.LoadFrom(dllPathName);

foreach (string clsName in lstClassNames){ 

refelectedType = refelectedAssembly.GetType(clsName);

ICommonRoutines routine = (ICommonRoutines)Activator.CreateInstance(refelectedType);
routine.RoutineOne(); }

Here also we have to type cast the created instance into ICommonRoutine interface . We now can read the type name from xml file and create the instances using the common interface , but to do so we have to reference the implementation dll (Implementation.dll) to get the type ICommonRoutines for typecasting .

As the new types are added in the xml file we have to create the type definition and implement the interface in order for our code to be valid for instance creation.

Another way to do above thing is to use Dynamic keyword which removes the dependency on the implementation dll and bypasses the compilation time checking.

         Type refelectedType;
         Assembly refelectedAssembly = null;

refelectedAssembly = Assembly.LoadFrom(dllPathName);

foreach (string clsName in lstClassNames){ 

refelectedType = refelectedAssembly.GetType(clsName);

dynamic routine = Activator.CreateInstance(refelectedType);
routine.RoutineOne(); }

Using Dynamic keyword removes the need for typecasting during instance creation and dependency on implementation dll.  Now we can add any number of types in xml file and create their instances without worrying about availability of types for typecasting . As the compile time checking is ignored , it becomes little dangerous if all the types are not properly implemented . In this case it is still better to use an interface as a contract for all the types to follow and use dynamic keyword .

No comments:

Post a Comment