What is the difference between Invoke and DynamicInvoke in delegates? Please give me some code example which explain difference between that two methods.
What is the difference between Invoke and DynamicInvoke in delegates? Please give me some
Share
When you have a delegate instance, you might know the exact type, or you might just know that it is a
Delegate. If you know the exact type, you can useInvoke, which is very fast – everything is already pre-validated. For example:However! If you just know that it is
Delegate, it has to resolve the parameters etc manually – this might involve unboxing, etc – a lot of reflection is going on. For example:Note I’ve written the
argslong hand to make it clear that anobject[]is involved. There are lots of extra costs here:MethodInfoBasically, avoid
DynamicInvokewhen-ever you can.Invokeis always preferable, unless all you have is aDelegateand anobject[].For a performance comparison, the following in release mode outside of the debugger (a console exe) prints:
Code: