I have been advised to refrain usage of Reflection. I really wanted to know, is it because Reflection is expensive? If not what is the reason to avoid using it?
My current and future projects might as well need to access any given class – members information. As I some times need to list fields & Properties – values or declaration name.
So What I would like to know, is:
How does Reflection work? How does it get to the information? (a short explanation will do)
And why it’s not so recommended to use the reflection in application? If you do need to get required information being a field or properties values OR names, could you do it not by using System.Reflection?
Some background.
The usage in my current project(for example), is to list out a specific sql server table-columns names, or SQL – tables names .
I could think of other ways to have it returned as a List<strings>.
If I really knew why or how “bad” it is, to use reflection,
..I could then make a decision, if I really want to avoid it, as I might find an alternative approach (in this specific scenario).
Either by accessing database (not preferable) any time I need (say) list of tables names or I could do it once (access the data ), then store it in a text file or xml, if I really must avoid reflection.
I know of some even more elegant one, too. That’s not the issue though. (again this is only an example as there could be many other use cases, you probably know that. )
update
this Question was closed please help reopen it , and vote ‘reopen’ below
thanks .
A CLR assembly (executable or dynamic link library) typically contains metadata about its structure, which means information about the types, the structures, the methods, the fields, their names and a bunch of other information that, in “traditional” languages, were usually lost and replaced by offsets and size information only.
Reflection is a powerful tool and some (usually advanced) things can only be achieved using reflection. However, it also raises concerns about security and encapsulation, because you start relying on the implementations of the parts of your program (or other programs), while you should generally avoid those and just trust the interface those parts give. Another concern is performance, because to access all this kind of (ultimately textual) information, the program is slowed down, in contrast to using the non-reflective approaches (which usually still use offset and size information). For instance, you could re-implement polymorphism using reflection and bypass the virtual method table. But the latter is many times faster than the former.
Use reflection if you have to, but don’t use it if you don’t have to. It’s one of those tools that are very powerful and people advise against using them, but you may use them if you really know what you’re doing. That being said, please keep in mind that using reflection techniques in a wrong manner does not only raise the above issues, but tends to make your code significantly harder to maintain as well.