How do you optimize the performance of getting / setting attributes?

In C #, I mark the properties of some classes with attributes, and I use reflection to find these properties to execute the get and sets commands. However, I found that getting / setting with reflection in this way is about 10 times slower than POCO gets / sets. In addition to abandoning the fundamental scenario described above for using alternative methods, are there any documented tricks to make this significantly effective, such as caching methods?

+3
source share
2 answers

Going beyond what is said casperOne (including bits of verifying that this bottleneck), you may be very useful to convert the recipients / setters in delegates (a Func<T>and Action<T>for the recipient and setter, respectively), using Delegate.CreateDelegate . This can be of great importance, and it is not very difficult. If you are already going to cache PropertyInfo, just cache a couple of delegates.

I have a blog post about Delegate.CreateDelegate - I used it for the first time in anger when porting protocol buffers (which may be a reflection - difficult at times). In this case, it helped.

+7
source

, PropertyInfo , . , PropertyInfo , .

, , (, PropertyInfo , ), .

, , , . , GetProperty/GetProperties Type .

+3

All Articles