开发者

performance difference while calling method dynamically

开发者 https://www.devze.com 2023-03-16 06:52 出处:网络
Is there a big performace difference between these 2 sample method calls ? SampleClass sc = new SampleLib.SampleClass();

Is there a big performace difference between these 2 sample method calls ?

SampleClass sc = new SampleLib.SampleClass();
sc.DoSomething("Hello");

dynami开发者_如何学JAVAc dyn = someAssembly.CreateInstance("SampleLib.SampleClass")
dyn.DoSomething("Hello");

Lets assume that

dynamic dyn = someAssembly.CreateInstance("SampleLib.SampleClass")

is not frequent call. Once it creates the instance it will continue intensivly work only with created instance.


The only way to know for sure is to profile in your context. To set expectation, dynamic is pretty smart, and caches (per pattern) the actual code-path. As such, it is much faster than raw reflection, however an interface should be slightly faster and has the advantage of static checking.

Personally, I'd code to an interface from a library dll that both the current code and SampleLib can reference, i.e.

IMyInterface foo = (IMyInterface)someAssembly.CreateInstance(
                         "SampleLib.SampleClass");
...
foo.DoSomething("Hello");
0

精彩评论

暂无评论...
验证码 换一张
取 消