Reflection.Emit better than GetValue & SetValue :S

后端 未结 5 1513
说谎
说谎 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:15

    If you're fetching/setting the same property many times, then using something to build a typesafe method will indeed be faster than reflection. However, I would suggest using Delegate.CreateDelegate instead of Reflection.Emit. It's easier to get right, and it's still blazingly fast.

    I've used this in my Protocol Buffers implementation and it made a huge difference vs PropertyInfo.GetValue/SetValue. As others have said though, only do this after proving that the simplest way is too slow.

    I have a blog post with more details if you decide to go down the CreateDelegate route.

提交回复
热议问题