Fog Creek Software
Discussion Board

Welcome! and rules

Joel on Software

What are the practical benefits of REFLECTION?

I hear a lot about .net's reflection capabilities. I've read a little about it.

It sounds like it's primarily to allow one program to sort "peer" into another .exe's types, methods, etc.

So, rather than needing to explicitly make your app an (OLE?) server as you'd do in the "old days" to let another program "control" your program, you can use reflection?

Thus, one program can "use" another program.

Is this the primary benefit?

Mr. Analogy {ISV owner}
Wednesday, January 26, 2005

In general it means you can do stuff a lot more dynamically than otherwise. I believe the GUI junk in products like VS.NET is based on reflection so they can figure out at runtime what controls are available; as opposed to having some static/nasty registry entry or INI file type thing.

The only time I actually used Reflection was in Java where we had classes to do business-specific things, and then a controlling class that could manage all the business-specific classes in a generic way without hardcoding the names or methods, etc. of them.

Reflection seems to be one of those things like Threading where everyone with experience of it says 'Dont use it unless you absolutely have to'.

In .NET there is also Reflection stuff to do with emitting MSIL at runtime, but I don't know much about that stuff, sounds cool in a 'I will never use that' sort of way.

Wednesday, January 26, 2005

As far as I know, .NET doesn't have an equivalent to OLE Automation for EXEs.  However, reflection does provide something similar to COM/OLE typelibs.  Development environments can use reflection to get .NET component information as they would use typelibs for COM/OLE components. 

Besides that, there are a wide variety of uses for reflection.  I've used it for things like logging (it's easy to dump out the values of all the properties for a given object using reflection) or just learning about a components (I've used it to derive new classes from pre-existing controls in order to monitor event order).  I think the only production code I've ever used it in was some custom binding of business objects to UI elements. 

The .NET Framework uses it all over the place -- for example, XML serialization, data binding, the PropertyGrid control.

Wednesday, January 26, 2005

I think of Relfection as an interesting idea that will never really be used much. At least not by me while developing ASP.NET / SQL Server business applications.

I find it a bit worrying that you can use Reflection to pry into classes at runtime, and call private methods, or examine values of private member variables.

I wish I could think of a good use for it, but so far that has eluded me.

Thursday, January 27, 2005

Reflection is useful for bolting together components without having to implement some sort of master table of what's available.

Example: One executable with generic functionality, many DLLs with specific functionality. If the executable uses reflection to determine the available DLLs, you can add or remove DLLs at will in your Visual Studio solution. The present set will be recognized automatically.

Another very popular use for reflection: external tools that operate on your code. Lutz Roeder's Reflector, Microsoft's FxCop, and NUnit are all based on reflection. Reflection allows direct access to the type metadata of all assemblies, which means .NET tools don't have to parse the source code, unlike C++ tools.

The comparison with threading is apt, though. You won't normally need reflection, unless you're actually writing a code analysis tool. Reflection is verbous to write and slow to execute, so only use it when necessary (and that's not often the case).

Chris Nahr
Thursday, January 27, 2005

I've used reflection to alter object state for clients with "special requirements" -  For those clients I use reflection to compare any objects public properties against an XML file with a comparable structure, then if all the properties match, reset the object state using another lump of XML.
I only use it for specific clients, where they have requested unusual behavior and understand it may effect performance.

Jon Spokes
Thursday, January 27, 2005

I've used reflection (in both .NET and Python) to implement a "dispatcher" that can invoke methods at run time without having to know anything about them at compile time.  It adds great flexibility at the cost of some performance (but in the situations where I've used it, it has been worth it).

Thursday, January 27, 2005

Reflection can be used to implement very flexible plug-in architectures.

At run-time, you can load an assembly, inspect it for classes that match your particular kind of plug-in, and allow your main program to "learn" what capability the plug-in exposes.

Dave Mays
Saturday, January 29, 2005

It's worth noting that reflection is important if you want to define your own custom attributes.  Without reflection, you could not "read" the attributes at run-time. 

Since most other languages don't have attributes, this particular use of reflection is almost unique to .NET  (AFAIK Java 1.5 is the only other language with attribute-like features.)

John Rusk
Monday, January 31, 2005

I use reflection in .NET applications to make generic sorting objects for collections. It can take a property name when the sorting class is created and as long as that property exists for the class then the collection can be sorted.
This is nice when you actually have a reason to have different objects in a collection, but they all support say the Name property.

Nice when you don't really have access to change the underlying classes to implement some interface etc.

Also good for a plug in type architechture.

Monday, February 21, 2005

*  Recent Topics

*  Fog Creek Home