views:

649

answers:

9

Many .Net interview question lists (including the good ones) contain the question: "What is Reflection?". I was recently asked to answer this in the context of a 5 question, technical test designed to be completed in 15 minutes on a sheet of blank paper handed to me in a cafeteria. My answer went along the lines of "Reflection allows you to discover the Methods, Properties and Fields of any object at runtime". In retrospect, my answer explains how you can use reflection, but it does not explain what reflection is. In my view, my answer was sufficient to convey that I understand what reflection is for but didn't go so far as to explain what reflection is.

So please, in the context of .Net, in your own concise words, define what Reflection is. Don't spend more than three minutes answering. Don't refer to the .Net documentation, we've all seen it.

A: 

Reflection is the process by which an object can inspect and modify it's behaviour at runtime.

Rolo
object modifying itself ? that's not a general definition of reflextion at all.
Hassan Syed
+22  A: 

Reflection is the ability to query and interact with the type system in a dynamic way.

Brian Rasmussen
That's nice! Very concise and "dynamic" says a lot.
grenade
But then, is a method's code part of the type system? It seems to me that you interact with more than just the type system
Vinko Vrsalovic
@Vinko: Methods are defined in the context of types, so I would say yes, but I see your point. To be honest I didn't expect to cover all the details of reflection in a single sentence.
Brian Rasmussen
It's concise, but it is very close to the original answer in the text of the question.
Guge
@Guge: Reflection is more than discovery. You can invoke methods and get/set fields as well, which is what I tried to cover with "interact".
Brian Rasmussen
+7  A: 

Reflection is the CLR's awareness of code-level objects such class names, methods, etc. that is exposed via an API, namely System.Reflection, which allows a developer to leverage the runtime's cognizance of this information in their code.

Rule violation: I've edited this answer from its original form for the sake of accuracy.

antik
Reflection does not require the compiler at runtime.
Guge
Is it compiler's awareness or CLR's awareness?
Vinko Vrsalovic
In fact, I like this answer changing compiler for runtime.
Vinko Vrsalovic
Thanks for the pointing that out: I had it in my head that it was a compile-time thing but thinking further on it, I've capitalized on it frequently in frameworks that have absolutely no compile-time awareness of my code. One example is NHibernate.
antik
+3  A: 

Reflection is like naval-gazzing for code.

Brian Ensink
do i even want to know what naval-gazzing is?
Pondidum
something that involves refueling navy vessels.
Jim Ferrans
oh naval as i ships...the only thing i could think of was belly-buttons...
Pondidum
Actually, I think it should have been "navel-gazing" or "Omphaloskepsis" :-)
Joey
definition of Naval gazing: "Excessive introspection." source: http://www.urbandictionary.com/define.php?term=naval+gazing
Chad
I might have had better results with this answer than my own...
grenade
Haha, only Johannes spelled it correctly... everybody else is confused.
Dan Tao
+1  A: 

a form of introspection i.e. the ability to write code that queries code

jk
+3  A: 

I like your answer but I would also mention that Reflection is also a way of getting/setting private/protected fields/properties, that would otherwise not be available at runtime.

tzup
Reflection does enable this but that's not one of Reflection's redeeming qualities IMO. Fields are set to private for a reason: you're violating encapsulation using Reflection as such so it should be avoided if at all possible. Also, this answer is how to use reflection, not what reflection is.
antik
I would usually agree with your reasoning, but also think about the situation where you are working with a 3rd party component that you have no other access but by hacking into with Reflection. And yes, you're right, I just wanted to mention something that has not been said in the 5 previous answers. I'd rather add something unsaid to the thread instead of repeatingm, even if this means diverging a bit from the question...as long as I can add value to the discussion :)
tzup
I would agree it's not pretty but sometimes you really need something from a third party control. I found a memory leak in one and they wouldn't patch it for months so used reflection to temporarily do it myself.
PeteT
+2  A: 

During compilation of a .Net language, the compiler puts information about the program into the program file. This information can be used by the program itself or by other programs to find out which classes the program contains, what their methods, properties, fields and events are. Classes and their methods, properties and so on can also be used through reflection, even if the other program knows nothing about them before running. This allows different programs to be loosely coupled and makes all sorts of exciting programming possible. Reflection can also be used to build additional classes in running programs or in program files.

Guge
A: 

Reflection is the Resume of Code.

Gilligan
+2  A: 

Reflection is the ability of a program to handle itself as data.

Amnon