Windows DevCenter    
 Published on Windows DevCenter (http://www.windowsdevcenter.com/)
 See this if you're having trouble printing code examples


Analyzing Assemblies with Reflector

by James Avery, Jim Holmes
02/20/2007

You'll often need to understand how a class, method, or entire assembly fits into your overall system. Maybe you're reliant upon a third-party library and you're not sure exactly what it depends on. Perhaps a legacy chunk of your own code wasn't as well documented as you'd like, so you're not sure what features of the API you can use. You're left with an unclear picture of how parts of your system interact with each other and the capabilities offered—never a good situation.

Lutz Roeder's Reflector can help clear up situations like this. Reflector lets you explore any .NET assembly to find its dependencies and callers, or dive down and explore an entire assembly. Additionally, you can use tools within Reflector to decompile an assembly to see how the code was written.

Reflector at a Glance

Tool Reflector
Version covered 4.2.42.0
Home page http://www.aisto.com/roeder/dotnet/
Power Tools page http://www.windevpowertools.com/tools/92
Summary A great tool for exploring APIs in an assembly and learning more about how that assembly is used
License type Freeware
Online resources Bug tracker, message board
Supported frameworks .NET 1.0, 1.1, 2.0;Compact Framework (CF) 1.0, 2.0
Related tools in this book ILDASM, DILE, Reflector.Diff, Peli's Reflector Addins

Getting Started

The Reflector download is a simple .zip containing a Readme file and the actual executable. Drop the executable in a convenient folder and fire it up. (Reflector runs just fine under a non-Administrator account.) Reflector works with all versions of the full and compact .NET Frameworks. The first time you start Reflector, you'll be prompted to select which version you want loaded (Figure 1).


Figure 1. Selecting the initial set of assemblies

Using Reflector

After you've selected which Framework to load, Reflector will populate its display with the applicable assemblies. At this point, you're ready to open the assemblies you want to examine.

Load additional assemblies by dragging them onto Reflector or via the File→Open menu command. Reflector's main pane provides a tree-based display for browsing loaded assemblies. A separate pane opens up on the right when you're using additional features, such as when viewing documentation, analysis information, or an assembly's source code.

Browsing Assemblies

Expanding and browsing through an assembly shows you all the information about that assembly, such as its references, contained namespaces, and physical location. You can look through each class and see not only public or internal-scoped items, but private members as well.

Tip

The "-" node shows some rather obscure internals. Here you'll find a few hidden details about classes, such as types automatically generated by the C# compiler, C++ global methods in the Module node, and various bits of versioning information plopped in by the CLR. None of this is commonly usable, but it's there. You can most likely use the knowledge to win CLR and assembly trivia games at parties.

Figure 2 shows the Documentation window, accessed by pressing F1 or selecting View→Documentation, displaying comments on the CodeExporter class of the System.Xml.Serialization namespace.


Figure 2. Browsing the System.Xml namespace

Tip

Documentation is available only if you've first created comments using the summary elements for classes, methods, properties, and so on. You'll also need to create the XML comments file by checking the "XML documentation file" option in the Output section of the Build tab in the Project Properties window.

Links in documentation from elements such as seealso will show up as hyperlinks, enabling you to quickly move between related classes, as long as the developers have helped you out with good documentation!

Analyzing Interactions

You can research system interactions by using the Tools→Analyze (Ctrl-R) option to display all the dependencies of a class or method, as well as who is calling that item. This helps you to develop a great understanding of the system's dynamic behavior. Figure 3 shows what methods make use of the Find( ) method selected in the browser pane. Such information is very useful, as you can see how various callers are making use of any particular resource in an assembly.


Figure 3. Dependencies and callers for a method

Finding Help

Reflector also greatly improves your ability to do quick research on classes or methods. Highlight a method in the browser pane and select Tools→Search MSDN (or press Ctrl-M), and the appropriate MSDN help page will pop up in the righthand pane (Figure 4). Tools→Search Google (Ctrl-G) does the same thing with a Google search.


Figure 4. MSDN search results right inside Reflector

Searching Assemblies

Some assemblies and namespaces contain an enormous number of members. Sometimes it's nearly impossible to find the exact class or method you're looking for. Pressing F3 will launch Reflector's Search function and populate the righthand pane with the member names of all loaded assemblies. You can quickly narrow down the results by typing in parts of a search phrase in the Search field. Figure 5 shows a search for members whose type names or namespaces contain the text "validation."


Figure 5. Using Reflector's Search feature

Doing the Disassembly Thing

Another benefit of Reflector is its ability to disassemble compiled assemblies. Selecting an assembly in the browser pane and pressing the Space bar or selecting Tools→Disassembler will reveal the source code used to create those assemblies (Figure 6). This is very handy if you've gotten an assembly from a third party and you need to understand its inner workings to better use it.


Figure 6. Disassembling a method from ObjectMapper

Tip

This is a powerful feature of Reflector, but it's also easily misused. Just because you have a tool that can reveal someone's intellectual property doesn't mean you should use it for that purpose. (And if you do, you might be breaking the law!)

Extending Reflector

One of the best features of Reflector is the amazing wealth of add-ins that have been created for it (Roeder maintains a list at http://aisto.com/incoming/reflector/addins/). There are Reflector add-ins to help you with everything from differencing files to creating code metrics for assemblies. Be sure to read section 9.1 in Chapter 9 of Windows Developer Power Tools for examples of some of the cool extensions available for Reflector.

Reflector in a Nutshell

Reflector is perhaps the single most important tool for .NET developers. Sure, it will disassemble files if you need it to, but its primary focus is to help you learn how to best use the assemblies you have on hand. It's multipurpose, it's powerful, and it's a tremendous aid for research and education. Reflector and its add-ins help thousands of developers around the globe each day.

James Avery has been programming with Microsoft technologies for the last 7 years and has been working with .NET since the second beta release.

Jim Holmes has nearly 25 years experience in the IT industry, including network management, systems analysis, and software development in Perl, Java, C++, and .NET.


Return to WindowsDevCenter.com.

Copyright © 2009 O'Reilly Media, Inc.