Speeding up Reflection API with delegate in .NET/C#

前端 未结 5 1592
死守一世寂寞
死守一世寂寞 2021-01-02 16:08

This post has the comment if you need to call the method multiple times, use reflection once to find it, then assign it to a delegate, and then call the delegate.

5条回答
  •  悲哀的现实
    2021-01-02 16:56

    A delegate is simply a pointer to a function. If you're using reflection (at all) there is generally a lot of overhead associated with it. By finding this methods address once and assigning that address to your delegate variable, you are in effect caching it.

    So, it's not the "delegate" type that works faster, it's just that you're "computing" once and "using" it multiple times that grants you the speed increase.

提交回复
热议问题