Introducing .NET Reflector

I gave a presentation on .NET Reflector at the January 12 meeting of RockNUG.  I took most of my time demonstrating the product and answering questions, so I had very few slides.  So instead of simply posting them here and calling it a day, this blog post will incorporate some of the Q & A that happened during the presentation.

What Is It?

The title slide in my presentation called .NET Reflector an “x-ray machine for (unobfuscated) .NET assemblies”.  That little phrase actually understates what .NET Reflector can do.  Decompiling .NET assemblies is only one of the three major things it does (and is probably the best-publicized of its capabilities).  This tool also provides an excellent class browser and static analysis capabilities.

Why Is It Helpful?

In addition to giving developers to see code for third-party assemblies when they don’t have access to their source files, it can be quite a useful learning tool for seeing what the compiler generates on-the-fly for the source code we write ourselves.

Demos

One of the first features I demonstrated was the ability of .NET Reflector to decompile an assembly into multiple .NET languages.  C# is the default, but VB.NET, Delphi, Oxygene, MC++, F#, and MSIL are the other target languages available out of the box.  Using the add-in architecture of .NET Reflector, one developer added limited support for decompilation to PowerShell (download the PowerShellLanguage.zip file).

Around this point in the presentation, someone asked if you could cut-and-paste decompiled source into a file.  Not only does it work, but with Denis Bauer’s Reflector FileDissassembler plug-in installed, you can decompile an entire assembly into its constituent source code files (though I suspect that Red Gate Software would prefer that you pay for .NET Reflector Pro to get this capability). 

I was also able to demonstrate the much-overlooked static analysis capabilities of .NET Reflector.  They enable developers to learn what code depends on particular methods, where they’re used, what code exposes them, and where they’re called.  It turns out that there’s a free plug-in which extends this capability even further.  The Dependency Structure Matrix (DSM) plug-in allows developers to generate and manipulate matrices for determining the level of complexity in a software architecture.  Considering that a full-featured tool with DSM capability like NDepend costs hundreds of dollars per seat license, even a fraction of those features from a free .NET Reflector plug-in are well worth the time to learn how to leverage.

More Q & A

When I showed that .NET Reflector pulled in source code comments in its display of disassembled code, one member of the audience pointed out (correctly) that this was only possible because the XML file containing the comments was co-located with the assembly.  When I tested the disassembly capability afterwards without the XML file present, the comments didn’t display.

There was also a question from the audience about how .NET Reflector compared with ILDASM (which ships with the .NET Framework).  The short answer is that ILDASM is far more limited by comparison.  It only decompiles to IL, it lacks the analysis capabilities of .NET Reflector, and most importantly it doesn’t have a plug-in architecture to enable expansion of what it can do.

Conclusion

My presentation on January 12 only scratched the surface of what .NET Reflector can do.  I hope this post has added more depth, and piqued your curiosity to use the tool to improve your own development experience.  You may find the following links helpful in leveraging .NET Reflector for your own development work:

Candied Sweet Potatoes

Last month (for Thanksgiving), my dad, sister, a few cousins and I cooked so mom could have a break. The food turned out so well, she asked us to do it again for Christmas–including a candied sweet potato recipe I cobbled together from different ones on the internet. The recipe is below.

Ingredients
4-5 lbs of sweet potatoes
1 20oz can of crushed pineapple
1 1/2 cups of dark brown sugar
1 1/4 cup of I Can’t Believe It’s Not Butter
cinnamon, nutmeg, & ginger to taste

Instructions
Fill an 8qt pot 2/3rds of the way with water. Bring to a boil. Boil the sweet potatoes for 15 minutes. Drain and set aside to cool for peeling and slicing later.

In a saucepan, melt the I Can’t Believe It’s Not Butter, add the brown sugar and stir. Add cinnamon, nutmeg, and ginger until the sauce tastes how you want it.

After you’ve peeled and sliced the sweet potatoes, lay down a layer of them, drizzle/smear on some of the sauce, and sprinkle on some of the crushed pineapple. Repeat until you’ve filled whatever casserole dish or foil pan you’re using.

Bake this at 425 degrees for 90 minutes.

The problem with exit interviews

The biggest problem with exit interviews is that they’re too little, too late. I had an exit interview recently (since I accepted an offer to go elsewhere), and there wasn’t anything wrong with the questions–it was just that nothing could be done about any of the concerns I raised.

The second major problem with exit interviews is that they focus too narrowly. All the feedback from exit interviews comes from people who’ve decided to leave. Assuming a company has had relatively low turnover for awhile, the feedback could be leaving out information from as much as 90% of its workforce.

If a company is serious about employee retention, they need to get feedback from as much of their workforce as possible on a regular basis. In my exit interview, I got questions about benefits, commute, holidays, and other issues. Regular, anonymous surveys on those issues would probably reveal a lot of useful information about ways benefits could be improved. Gathering this kind of information regularly will mean that at least some (if not most) of the answers you get will be from people who still have a stake in the company’s future.

Filtering Heterogeneous Arrays in .NET

One of the bugs I was recently asked to fix for an application required me to determine whether or not to display one of the members of a list.  This proved somewhat challenging since the lists in question were heterogeneous (two difference subtypes of an abstract base class).  It turned out that LINQ provides a nice solution to this sort of problem in the form of the OfType<T> method.

Given an IEnumerable collection with elements of multiple types, calling OfType<T> on the collection where T is the desired type will return a collection containing only elements of type T.  Before learning about OfType<T>, I’d been using the Cast<T> method.  This was fine as long as all the collection elements were of the type T I wanted.  The moment this wasn’t the case, my LINQ query threw a cast exception.  OfType<T> seems to work similarly to the “as” operator in C#, in that it doesn’t complain if a list element isn’t type T–it simply excludes it from the returned collection.

PowerGUI and .NET Framework 4.0

On my current project, we use PowerShell scripts to automate our UI testing.  We’ve been writing and running the scripts in the PowerGUI Script Editor, an excellent tool that’s also free.  When we upgraded our application to run on version 4.0 of the .NET Framework from 3.5, we lost the ability to run PowerShell scripts in debug mode from PowerGUI.

The only work-around for this I’ve found (at least until a version of PowerGUI built on .NET 4.0 comes out), is a registry hack that forces all the .NET apps on the machine to use the latest version of the CLR.  You can find more details in this user discussion at powergui.org, or this discussion on stackoverflow.com.

ScrollViewer+ItemsControl vs. ListView

One of my most recent tasks at work was determining the cause of slow performance in one part of an application and coming up with a fix (if possible).  We tracked the source of the problem down to a use of ItemsControl inside a ScrollViewer.  Because the ItemsControl instance was trying to display hundreds of complex items, it took a noticeably long time to load.  This turns out to be a known issue, with a few possible solutions.  Simply changing the ItemsPanelTemplate of the ItemsControl instance to contain a VirtualizingStackPanel didn’t fix our performance problem.

What did resolve our performance issue was replacing the ScrollViewer and ItemsControl combination with a ListView.  The list of what we changed includes:

  • Giving the ListView the same name as the ItemsControl.
  • Giving the ListView the same ItemsSource as the ItemsControl.
  • Update the ItemsPanelTemplate of the ListView to use VirtualizingStackPanel.
  • Set HorizontalScrollBarVisibility to “Disabled”.
  • Bound the Visibility property of the ListView to a Converter.
  • Update the ItemContainerStyle with a ListViewItem style that sets the HighlightBrushKey and ControlBrushKey to be transparent.

Note: The last of those steps does override those styles for the entire application, so it may be best to skip it unless it doesn’t negatively impact the look-and-feel of the rest of your application.

The changes we made reduced the load time from around 20 seconds down to less than 2 seconds for 400 items.

The tradeoff in moving to a ListView (with VirtualizingStackPanel) from ScrollViewer+ItemsControl is scrolling speed.  Scrolling through 400 items does go more slowly, but it’s preferable to waiting as long as we did just to see the data.

Bloatware happens when you aren’t the only customer

This article in Ars Technica reminded me of one of the things I never liked about PCs you bought from Dell, HP, or any major vendor–bloatware.  Every PC I’ve had that I didn’t build myself, and every Windows laptop had multiple pieces of software that I didn’t want.  The least-offensive of these apps merely took up hard drive space.  The worst made the computer slower and more of a hassle to use.  Switching from Windows to Mac at home meant no more bloatware.  Moving to an Intel chip-based Mac extended the no-bloatware experience to Windows VMs.  Moving to the iPhone a couple of years ago (and sticking with it by buying an iPhone 4) seems to have spared me from vendor bloatware as well.  This is especially important in the mobile space because today’s smartphones have a lot less storage space to waste, and less-powerful CPUs than modern PCs.

Bloatware happens on the PC because even though we buy them, we aren’t the only customer.  Every company with some anti-virus software to sell, a search engine they want you to use, or some utility they want you to buy wants to be on your PC or laptop.  They’re willing to pay to be on your new machine, and PC vendors aren’t going to turn down that money.  A similar thing seems to be happening on Android phones now.  Here’s the key quote from the story:

“It’s different from phone to phone and operator to operator,” says Keith Nowak, spokesman for HTC. “But in general, the apps are put there to meet the operator’s business and revenue needs.” (emphasis mine)

The money we pay for our voice and data plans isn’t the only money that Verizon, AT&T, Sprint, & T-Mobile want.  Some of these carriers have decided that they’ll take money from other companies who want to put applications on the smart-phones they sell.  This highlights one of the key differences between the way Google approaches the mobile phone market and the way Apple does it.

When it comes to Android, you and I aren’t Google’s customers–not really.  The real customers are mobile phone hardware vendors like Motorola and HTC.  They need to care how open and customizable Android is because they expect it to help them sell more phones.  Making the OS free for the vendors is in Google’s interest because the bulk of their revenue comes from advertising.  The more phones there are running Android, the more places their ads will appear.  Android’s openness is only of interest to us as users to the extent it allows us to do what we want with our mobile phone.

Unlike Google, Apple is in business to sell us electronics.  They expect iOS4 to help them sell more iPhones and iPads.  But since you and I are the customers Apple is chasing, no pre-loading of apps from third parties.  It doesn’t mean they won’t feature apps that highlight the phone’s capabilities (Apple does plenty of that).  Nor does it mean we can’t get apps from AT&T, just that putting them on and taking them off is our choice.  There is a tradeoff as far as how long iPhone users wait for features when compared with Android phone users.  But I think the iPhone features all work better, and work together to create arguably the best smartphone available.

My First PowerShell Cmdlet

We’ve been using PowerShell to write automated tests of the UI on my current project.  One of the tasks I took on today was creating a custom cmdlet to enable us to select radio buttons.

I already had an existing assembly of cmdlets to work with, so I just added a new class (SelectRadioButton) to it.  Next, I added references to System.Management.Automation and System.Windows.Automation. With these references in place, I could add this attribute to the class:

[Cmdlet(VerbsCommon.Select, "RadioButton", SupportsShouldProcess = true)]

The attribute determines the actual name of the cmdlet you’ll use in scripts (Select-Radiobutton).  The cmdlet needs an instance of AutomationElement to operate on, so that’s defined next:

[Parameter(Position = 0, Mandatory = true, HelpMessage = "Element containing a radio button control")]
[ValidateNotNull]
public AutomationElement Element { get; set;}

Finally, I adapted some of the logic for my override of the ProcessRecord from this article on using UI automation.  The end result looks something like this:

protected override void ProcessRecord()
{
try
{
if (Element.Current.ControlType.Equals(ControlType.RadioButton))
{
SelectionItemPattern pattern = Element.GetCurrentPattern(SelectionItemPattern.Patern) as SelectionItemPattern;
if (pattern != null) pattern.Select();
}
else
{
//Put something in here for handling something other than a RadioButton
}
}
catch (Exception ex)
{
// You could put some logging here
throw;
}

}

In non-iPhone 4 news

Apple stealthily revised the Mac mini.  Get the full story here, but the part I think is the most interesting is that they designed in a removable panel on the bottom to make it easy to replace the RAM yourself.  It shows a rare bit of flexibility from Apple when it comes to their hardware.

As for the rest of the device:

  • No more power brick?  Nice!
  • Tons of ports (including HDMI).
  • SD card slot
  • No Blu-Ray?  Rats.  “Bag of hurt” or no, that would have been nice.
  • The price bump from the previous version of the Mac mini seems a bit steep.

The original Mac mini was the first computer I ever bought from Apple.

When default settings attack

When you first install SQL Server 2008 Express, the TCP/IP protocol is disabled by default.  Be sure the protocol is enabled (which requires restarting the service) before you try to run an application that depends on it, otherwise you could spend hours trying to figure out why your application won’t work.  It looks like SQL Server 2008 R2 Developer behaves the same way.

I suggested this awhile back to a co-worker who’d been struggling all day with why an application wasn’t working, and it turned out to be the solution.