C# – When do you use reflection? Patterns/anti-patterns


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?

Best Solution

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.