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

前端 未结 5 1550
死守一世寂寞
死守一世寂寞 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 17:08

    Fist off, this is not caching. You are not saving a copy of the method in a "closer" location, you're just holding on to a reference to that method.

    Think about the steps needed to take in order to call a method using reflection (accessing the reflation data from the assembly, looking up the method/namespace/class by name and more...), the last step is getting a reference (and don't let anyone tell you that a delegate is a pointer!) to the method and invoking it. When you use a delegate you only take the last step, and save yourself all that headache that comes with reflection.

提交回复
热议问题