When do you use reflection? Patterns/anti-patterns

前端 未结 17 2099
旧时难觅i
旧时难觅i 2020-12-02 09:58

I understand the reflection API (in c#) but I am not sure in what situation would I use it. What are some patterns - anti-patterns for using reflection?

相关标签:
17条回答
  • 2020-12-02 10:44

    +1 on the factory pattern usage -- very powerful there.

    Aside from the factory pattern, every time I've used it, I probably shouldn't have...

    I've used it to dynamically load classes that implement certain interfaces (mine were menu items from assemblies) at startup and I really regret that usage. I wish I would've loaded from a config file (and later added a form that showed available interfaces to load). It's cool but terribly slow...

    An anti-pattern is to use it access properties that class designers marked as private without knowing why they marked them as private. I've done this with the DataGridView WinForms control to unset a boolean variable so I could have a "companion" column move when its complement was moved. Once again, it's very cool but that code will fail horribly if a new release changes that private property (it very well could be gone in 3.0 or 3.5...).

    0 讨论(0)
  • 2020-12-02 10:45

    In one product I'm working on we use it a lot, but Reflection is a complex, slow beast. Don't go looking for places to use it just because it sounds fun or interesting. You'll use it when you run into a problem that can't be solved in any other way (dynamically loading assemblies for plug ins or frameworks, assembly inspection, factories where types aren't know at build, etc). It's certainly worth looking at reflection tutorials to see how it works, but don't fall into the trap of "having a hammer and everything looking like a nail." It's got very specialized use cases.

    0 讨论(0)
  • 2020-12-02 10:48

    I use it in a case similar to that mentioned by Harper Shelby, in which a configuration file specifies at runtime which object to instantiate. In my particular case there's nothing as elaborate as a factory--only a set of classes that implement a common interface, and a simple function that reads the configuration file and creates the proper object, returning the interface.

    0 讨论(0)
  • 2020-12-02 10:49

    I use it in a binary serializer (protobuf-net). I use reflection only to build the model - when it is used (i.e. during [de]serialization) it is using delegates etc for maximum performance.

    I also used it (along with ComponentModel and Reflection.Emit) in HyperDescriptor, to build accelerated property access (~100x the speed of regular reflection).

    And by necessity, you need to use reflection if you are building your own Expressions.

    0 讨论(0)
  • 2020-12-02 10:49

    I went for 2 years of development without understanding the purpose of reflection. It has very niche uses, but is extremely powerful when it is the right tool for the job.

    I think what I'm trying to say is only use it when you are sure it's the only way to achieve what you want.

    0 讨论(0)
提交回复
热议问题