0

If use dynamic Key Word & we assign some Type on it, is Compiler perform boxing/un-boxing Operation? For Example;

dynamic myInstance=null;
object key="BankProject.Payment";
Type myType=ServiceCashe.GetType(key);//get desire type from cache...
myInstance=Activator.CreateInstance(myType); //instanciate myType
csefaruk
  • 193
  • 1
  • 3
  • 15

2 Answers2

5

Unless it's a value type, there'll be no boxing going on - but in the code sample you've used, there's no real use of dynamic typing anyway. Your code thus far is equivalent to:

object key = "BankProject.Payment";
Type myType = ServiceCashe.GetType(key);
object myInstance = Activator.CreateInstance(myType);

It's only when you perform dynamic member access - e.g. myInstance.SomeMethod() that dynamic typing would actually come into effect. Usually the way to avoid that is to make all the types that you're fetching dynamically implement some interface:

object key = "BankProject.Payment";
Type myType = ServiceCashe.GetType(key);
IPayment myInstance = (IPayment) Activator.CreateInstance(myType);
myInstance.SomeMethodInInterface();

Then the only "dynamic" parts are creating the instance, and then the execution-time check in the cast.

As always, if you have performance concerns you should measure them in realistic situations against well-defined goals. Even if this did perform boxing and unboxing, we would have no idea whether or not that was a significant cost in your context. (As it happens, boxing and unboxing are way cheaper in my experience than Activator.CreateInstance...)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

For reference, if you create method:

static void Test()
{
    dynamic myInstance = null;
    object key = "BankProject.Payment";
    Type myType = typeof(SomeClass);
    myInstance = Activator.CreateInstance(myType);
    int result = myInstance.SomeMethod();
}

this will be translated internally in something like (decompiled in ILSpy):

private static void Test()
{
    Type myType = typeof(SomeClass);
    object myInstance = Activator.CreateInstance(myType);
    if (Program.<Test>o__SiteContainer0.<>p__Site1 == null)
    {
        Program.<Test>o__SiteContainer0.<>p__Site1 = CallSite<Func<CallSite, object, int>>.Create(Binder.Convert(CSharpBinderFlags.None, typeof(int), typeof(Program)));
    }
    Func<CallSite, object, int> arg_AA_0 = Program.<Test>o__SiteContainer0.<>p__Site1.Target;
    CallSite arg_AA_1 = Program.<Test>o__SiteContainer0.<>p__Site1;
    if (Program.<Test>o__SiteContainer0.<>p__Site2 == null)
    {
        Program.<Test>o__SiteContainer0.<>p__Site2 = CallSite<Func<CallSite, object, object>>.Create(Binder.InvokeMember(CSharpBinderFlags.None, "SomeMethod", null, typeof(Program), new CSharpArgumentInfo[]
        {
            CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
        }));
    }
    int result = arg_AA_0(arg_AA_1, Program.<Test>o__SiteContainer0.<>p__Site2.Target(Program.<Test>o__SiteContainer0.<>p__Site2, myInstance));
}

Hence, as you see, there is much more code involved than you could think at first glance.

Konrad Kokosa
  • 16,563
  • 2
  • 36
  • 58