Reflection.Emit better than GetValue & SetValue :S

后端 未结 5 1516
说谎
说谎 2020-12-13 21:03

I\'ve been told to use Reflection.Emit instead of PropertyInfo.GetValue / SetValue because it is faster this way. But I don\'t really know what stuff from Reflection.Emit a

5条回答
  •  遥遥无期
    2020-12-13 21:28

    Use PropertyInfo.GetValue/SetValue

    If you have performance problems cache the PropertyInfo object (don't repeatedly call GetProperty)

    If - and only if - the use of reflection is the performance bottleneck of your app (as seen in a profiler) use Delegate.CreateDelegate

    If - and really really only if - you are absolutely sure that reading/writing the values is still the worst bottleneck it's time to start learning about the fun world of generating IL in runtime.

    I really doubt it's worth it, each of those levels increase the complexity of the code more then they improve performance - do them only if you have to.

    And if runtime access to properties is your performance bottleneck it's probably better going for compile time access (it's hard time to be both generic and super high performance at the same time).

提交回复
热议问题