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?
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.
Func<T>
Action<T>
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.
, PropertyInfo , . , PropertyInfo , .
, , (, PropertyInfo , ), .
, , , . , GetProperty/GetProperties Type .