event.Invoke(参数)与事件(参数)。这是更快?这是、参数、更快、事件

2023-09-03 02:41:17 作者:待在绿匣里的猫

这是更快;使用event.Invoke(参数),或者只调用事件(参数)。有什么不同?比另一个更快或更慢;或者是preference它只是一个问题?

Which is faster; using event.Invoke(args), or just calling event(args). What's the difference? Is one faster or slower than the other; or is it just a matter of preference?

推荐答案

someDelegate(...)是一个编译器速记 someDelegate。调用(...)。 他们都编译为相同的IL— A callvirt 指令的委托类型的调用方法

Writing someDelegate(...) is a compiler shorthand for someDelegate.Invoke(...). They both compile to the same IL—a callvirt instruction to that delegate type's Invoke method.

调用方法是由编译器为每个具体的委托类型产生的。

The Invoke method is generated by the compiler for each concrete delegate type.

相反, DynamicInvoke 方法的基础上定义的代理键入,使用反射调用委托和是缓慢的。

By contrast, the DynamicInvoke method, defined on the base Delegate type, uses reflection to call the delegate and is slow.