tags:

views:

619

answers:

8

As a programmer who is new to .NET and C#, I find that using .NET Reflector is an incredible utility to use to see how the "professionals" write their code.

Can anyone suggest good .NET based applications to use Reflector on for Desktop Applications - any application examples would be appreciated.

+1  A: 

Can't think of any off hand, but you might want to grab some open source stuff and compile it. This way you will have the source to refer to if the MSIL gets confusing.

I've found that Code Project has a lot of good sample programs available for various different purposes. Although, I mainly used it for C++ apps and I have yet to look into the .NET programs. My guess is that they are just as good though.

regex
A: 

I've typically used the Reflector to get answers to efficiency/performance-based questions. For example, I wanted to know if it's best to use == operator or .Equals() when comparing strings... or if it matters at all. Crack open the System.String assembly in the Reflector, and you'll get your answer by examining the method calls and operations that take place.

Kon
A: 

I think it's mainly useful for the .NET core and supplementary assemblies. For example, if you are wondering how Parallel.Invoke works, just pop open Reflector and become a little wizer. Beware though, that mscorlib has lots of P/invoke references that you won't be able to disassemble for obvious reasons.

Dmitri Nesteruk
Now we have the source code though! http://weblogs.asp.net/scottgu/archive/2008/01/16/net-framework-library-source-code-now-available.aspx
Mufasa
Wow, I've got to see that. I wonder if Microsoft made their OpenType API open-source... somehow I doubt it.
Dmitri Nesteruk
+1  A: 

I most often use it when I have a question about what a piece of code does in situations that aren't covered by the documentation. For example, I used to reflect A LOT when first attempting to create complex web controls for the old ASP.NET webforms model. That whole callstack is horribly complex and the documentation isn't the friendliest...

Probably the #1 reason why I use reflector now is to see what exceptions a call may throw. One of the biggest failings of the .net docs is that it generally only tells you what the current method throws in the way of exceptions. For example, if method A may throw exception 1, and method A calls method B which may throw exception 2, you'll only be told about exception 1 in the docs for method A. Sometimes you won't even get that with ArgumentNullExceptions.

One of the coolest reasons why I used reflector was when I wanted to create a DynamicMethod. DynamicMethods let you construct a method via IL calls; its inbetween reflection (slow) and compiling your own assembly in memory (fast execution but slow startup). I wanted to create a little class that checked to see if an event was fired during a test... Anyhow, I had no idea how to code IL, so what I did was create an assembly with a class that did exactly what I wanted to do in the DynamicMethod, compiled it, then viewed the IL in Reflector. That was a satisfying exercise in and of itself, and I may use the same techniques to do some more complex statement parsing/compiling down the road...

Will
Ditto re the IL; of course, in .NET 3.5 (and even more in 4.0), Expression is a viable (sanity-preserving) alternative to this, but you'll still need reflector to understand how to create an Expression ;-p
Marc Gravell
A: 

The main reason why I use it is to explore the efficiences of various .NET methods that essentially do the same thing.

+1  A: 

I use reflector during development to examine how the compiler turns C# into IL, as I do a lot of work with things like Reflection.Emit and Expression. I also use it to quickly look at my code; when you have a large dll library, I find it quicker to simply drag the dll+xml from the build server into reflector than it is to load each project into VS, especially if I'm not 100% sure where a bit of code is...

You can also use with with plugins like snippy as a test rig for snippets of code.

Marc Gravell
A: 

I tend to use Reflector lately to find what interfaces a class all makes use of. Sometimes it's helpful to analyze where a class is being used in other parts of a namespace as well to figure out how to implement it.

Looking at the code in Reflector can be helpful if the source code isn't available, but a vast majority of the time it's better to find the code itself because that will at least give you an idea how the code was actually written. Because Reflector is looking at the IL, the code is "optimized" for the computer, not human readability. Also if the developer took the time to add any comments to the code (I know it's rare, but it does happen) Reflector won't have those so any insight that's able to be gleaned from them wouldn't be there.

Agent_9191
A: 

Since our app uses several licensed components, I use Reflector to see exactly what licenses get embedded into the executable. Based on what I expect to see via Reflector, I also wrote unit tests to ensure the required licenses are present, even when built on the build server.

flipdoubt