Fog Creek Software
Discussion Board




definition of reflection

I keep hearing people talk about reflection and .NET.  I am a C/C++ guy, so this term is foreign to me.  From what I gather it just means obtaining what is typically compile-time info in C/C++ at runtime in other languages.  Like inheritance hierarchies, variable names, source code for functions, etc.  Is this the correct definition?

Roose
Saturday, February 21, 2004

Yes.

Li-fan Chen
Saturday, February 21, 2004

Not just obtaining that information, but usually you can use it in an IDispatch sort of way.  E.g. in Java, you can get a list of an object's methods, search them by name, and then invoke one (passing it the required args).  Sort of a clumsy "eval".
It's one of those things that you rarely need, but when you do need it you REALLY need it.

Brian
Saturday, February 21, 2004

It's also REALLY useful when building "plumbing" - automatically building proxies, serialization code, etc.

Chris Tavares
Saturday, February 21, 2004

Actually most of it can be done in C++.

It's certainly possible to build automatic serialisers and similar code, and yes it's within the language definition. It's probably possible to examine the inheritance tree of an object.

It's not SIMPLE, I emphasise, but then it wouldn't be in Java or C# if you didn't have the infrastructure already built for you.

The main problem with these techniques in C++ is that "int" and "char" and so on are not first-class objects.

Katie Lucas
Saturday, February 21, 2004

Reflection should be a language feature, not an API.

fool for python
Sunday, February 22, 2004

In Java, reflection lets you start with the name of a class and then create an instance of the class and invoke its methods. I've found it useful when I want to include a class name in a configuration file, so my code can instantiate any class that implements a particular interface.

Usually, it's best to avoid reflection if feasible. With reflection, you lose compile-time validation. Also, code with reflection is often more difficult to read and maintain. However, sometimes reflection is the only way to proceed.

Julian
Monday, February 23, 2004

*  Recent Topics

*  Fog Creek Home