In C# dynamic key word performance? -


if use dynamic key word & assign type on it, compiler perform boxing/un-boxing operation? example;

dynamic myinstance=null; object key="bankproject.payment"; type mytype=servicecashe.gettype(key);//get desire type cache... myinstance=activator.createinstance(mytype); //instanciate mytype 

unless it's value type, there'll no boxing going on - in code sample you've used, there's no real use of dynamic typing anyway. code thus far equivalent to:

object key = "bankproject.payment"; type mytype = servicecashe.gettype(key); object myinstance = activator.createinstance(mytype); 

it's when perform dynamic member access - e.g. myinstance.somemethod() dynamic typing come effect. way avoid make types you're fetching dynamically implement interface:

object key = "bankproject.payment"; type mytype = servicecashe.gettype(key); ipayment myinstance = (ipayment) activator.createinstance(mytype); myinstance.somemethodininterface(); 

then only "dynamic" parts creating instance, , execution-time check in cast.

as always, if have performance concerns should measure them in realistic situations against well-defined goals. if did perform boxing , unboxing, have no idea whether or not significant cost in context. (as happens, boxing , unboxing way cheaper in experience activator.createinstance...)


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -