.netreflection

Up to what extent should reflection be used?


We've got into a very tricky scenario in a project. We have used a lot of reflection in our project.

We have...

Like the above scenarios, we have used reflections at many other parts in our application. After using the reflection. we have noticed that the application is taking more processing cycles with reflection.

Up to what extent should we use Reflection in our project? What are the areas of a project that are most adversely affected by reflection in terms of processing? Where will reflection not make any performance impact? Are there any guidelines on using reflection?


Solution

  • Reflection is powerful, but it has drawbacks. In general, try to code without it—but it is hugely useful for "framework" libraries, where you have little/limited knowledge what the actual objects will be; for example:

    There are performance penalties with ad hoc reflection, but if you are going to do it lots (within your library, and in a tight loop) you can mitigate against this:

    And of course, any such code should be cached and reused (you don't want to compile+execute for every call; just the first—the rest should just execute).

    Or there are libraries that abstract this; for example, HyperPropertyDescriptor wraps custom IL (for member access) in the familiar PropertyDescriptor API—making it very fast, but painless. Your question mentions data-tables and entities; which sounds a lot like this previous question, where I did some metrics using HyperDescriptor, with summary (in milliseconds):

    Vanilla 27179
    Hyper   6997
    

    So my "take":