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");
精彩评论