Why is the use of reflection in .NET recommended?

后端 未结 9 659
再見小時候
再見小時候 2020-11-30 22:25

Is it definitely a good practice to use it?

What are some possible situations in a project that need reflection?

9条回答
  •  爱一瞬间的悲伤
    2020-11-30 22:48

    There are many uses for reflection:

    1. Iterating through properties in an object.
    2. Invoking a method that's defined at runtime.
    3. Many other other on the same vein.

    However, one of my favorite uses of reflection is to find properties that have been marked with attributes.

    For example, I've written attributes that mark which properties in my classes should be indexed using Lucene. At runtime, I can look at any class, and figure out what fields need to get indexed by just querying the class for "marked" properties.

提交回复
热议问题