0

I have little experience with Reflection in .NET (c#), but am trying to grok the best way(s) to use it. My understanding is that it is best used minimally, and within lower-level libraries, due to performance concerns. Is this true, and if not, why not?

ryanwebjackson
  • 1,017
  • 6
  • 22
  • 36
  • 1
    In my opinion (and hence why this is not an on-topic question for stack overflow), performance is secondary - by far - to the fact you lose all the type safety the language provides you. You'll find reflection at every level, not just low level libraries – Rob Jan 08 '17 at 22:27
  • Reflection, you do not get good intellisense helps, it avoids type checks, it is breaking OOP principles, like access to private methods, variables. It is way slower than the direct approach.But there are useful use cases, where architecture is important, where systems grow too complex,then it can substantially help in cases with inversion of control,but even there it can be avoided.It is easy to break during refactorings, no thread safety check at build time.Use when really needed or substantially simplifies life. – ipavlu Jan 08 '17 at 22:56

1 Answers1

0

Reflection is typically a "slow" process, as in not as fast as not using reflection, and I would say it is not recommended that you abuse reflection when it could be circumvented for more traditional means.

Some similar questions are here and here

As most people will say, it is often a fast enough solution. You need to run performance tests to determine if your use case is too slow or unnecessary.

As for where to use it, I wouldn't say it really matters, but abusing it in a view model or ASP.NET controller may not make sense as these components typically have concerns related to the UI. However, again, it is a case-by-case decision as to when reflection is necessary, so really you need to be the judge. There is no hard, fast rule saying reflection must only be used in lower-level libraries.

Reflection can solve many problems, and if it is the easiest solution I say go for it, just make sure to test your code. If the reflection ends up being to slow, refactor and try something else.

Community
  • 1
  • 1
Carson
  • 1,169
  • 13
  • 36
  • 1
    Thanks, this helps. Since this could be considered a duplicate, can it be marked as such? – ryanwebjackson Jan 08 '17 at 22:42
  • @ryanwebjackson I flagged it as a duplicate. It is under review. – Carson Jan 08 '17 at 22:43
  • @ryanwebjackson You should have a button to accept the proposed duplicate to immediately apply it- if you feel it is the same question – Rob Jan 08 '17 at 22:48
  • Oh dear, we can always all leave stackoverflow and say, that all was said and all you need is one answer 4 years old, and it will never change. ROFL – ipavlu Jan 08 '17 at 22:49
  • 1
    @ipavlu I'm not sure what point you're trying to make. Are you saying the mechanics behind reflection have significantly changed since that post was made? If so, you're free to go and post your own answer there. – Rob Jan 08 '17 at 22:53
  • I did. But remember, if it is all marked as duplicates. Then answers are blocked :). – ipavlu Jan 08 '17 at 23:00