Pandemonium

August 16, 2008

Tools of the Trade – Part Two: Reflector

Filed under: Tools and Software Development — bittermanandy @ 11:51 pm

I had originally been intending to write about something completely different in this second part of the Tools of the Trade mini-series, but I encountered a problem today that I simply could not have solved without Reflector. It reminded me of just how cool this tool is, and I just had to make sure everyone else knew how cool it is too.

Following on from my article about FxCop, I decided to have a crack at writing some custom rules to use alongside the default Microsoft ones. The first sign that this might not be as easy as I had assumed it to be was when I realised there was a total absence of documentation about it. There weren’t even many articles – the best ones I did find are here, here and here. I will cut a long story short by saying that the task of setting up my custom rules was more arduous than I expected. (That said, it’s working now and I’m very happy with the results).

I eventually reached the stage where I believed my custom rules library was ready to add into FxCop, but unfortunately when I attempted to do so, it complained that the XML file I had embedded as a resource in my Kensei.FxCopRules assembly was incorrectly formatted. And that’s about all it said – the error messages were not exactly verbose in their description of the problem. I spent a significant amount of time trying to work out what was wrong.

After some head-scratching, I realised that the default rules libraries that ship alongside FxCop must (obviously) contain a correctly-formatted XML file. If only there were some way for me to examine those XML files in the assemblies themselves! In the Bad Old Days, this might have been possible by loading the assembly into a binary viewer and scouring it by eye, looking for readable phrases; otherwise it would be a lengthy and painful process of reverse engineering. Happily this is Space Year 2008 and .NET has made everything so much easier.

Enter Reflector.

This wonderful little tool uses reflection (surprisingly enough) to analyze any .NET assembly you care to throw at it, and expose its innards in glorious detail. All I had to do to solve my problem was open up one of the FxCop assemblies, browse through it until I found the Resources section, find the XML file and click “View Resource”:

An FxCop rules library XML file viewed in Reflector.

An FxCop rules library XML file viewed in Reflector.

And there it was. A correctly-formatted FxCop custom rules XML file laid out in all its glory. From there it was mere seconds to see where I had been going wrong, and just a couple of minutes to fix it. I now have custom FxCop rules up and running and catching errors in my code – and I genuinely believe it would have been, if not impossible, at least very difficult and time-consuming without Reflector.

Of course, Reflector’s awesomeness doesn’t stop there. As well as showing you resources that have been embedded into assemblies, it can show you the code they contain as well. And I don’t mean “code” as in machine code, assembler, or even MSIL – I mean it can show you what is for all intents and purposes the same C# the assembly was originally written in. And it will do it for any assembly you care to throw at it. That’s pretty damn cool. To spell it out – if, say, you’re writing an XNA game and are curious as to how the actual XNA Framework code was written, just fire up Reflector and fill your boots:

The Microsoft.XNA.Framework assembly in Reflector.

The Microsoft.XNA.Framework assembly in Reflector.

If that’s not the single coolest tool anyone has written in the history of computing, I don’t know what is. And, again – just like FxCop, and indeed XNA and (should you not have the full version) Visual C# 2005 Express, it’s completely free. (Incidentally, if you’re wondering why I was inspecting BoundingBox.CreateFromPoints – all will be revealed in Part Three of this mini-series).

I feel as though I should spend longer explaining why Reflector is so amazing but honestly, if what you’ve seen already doesn’t excite you I’m not sure what will. (Perhaps the add-ins will convince you?) In any case, it is unquestionably a tool that any XNA (or indeed C# or .NET) programmer should be familiar with, and you will probably find that you use it often. I certainly do.

“Like the carpenter, the samurai looks after his own tools.” – Miyamato Musashi

Advertisements

9 Comments »

  1. Yep, this is definitely one of the greatest .NET tools. I can’t imagine coding without Reflector since I discovered it. It would be really great if Microsoft included its functionality into Visual Studio, so for example when you clicked “Go To Definition” it would also allow you to view code.

    Comment by Luke — August 17, 2008 @ 2:22 pm | Reply

  2. Hey, FxCop from your last post was a great tool, and this one looks just as good. I was astounded to see all the errors and warnings FxCop was throwing me after I ran it the first time, but it definitely improves code. And the ability to disable certain rules as well as add in your own makes it an amazing flexible and powerful tool that I don’t think I’ll ever program without.

    However, with the Reflector I’m just not getting it to work for me. The Disassembler shows nothing actually disassembled. ie, if I go to a method it simply shows the method signature and open and close curly braces below it: no code. Imagine if you took that BoundingBox sample screen shot but removed the actual method code it displays and you’ll have what I’m getting. Anyone encounter this problem before. I tried searching for help topics on this but can’t find any.

    Comment by Kolo Rahl — August 17, 2008 @ 10:43 pm | Reply

  3. I’ve not seen that myself, but with all of these tools it’s worth double checking that you’re running it with Administrator permissions. (That is, either with an Administrator account, or right-clicking and selecting “Run as Administrator”).

    Otherwise… I guess you should check that it’s the latest version, that you have the latest version of the .NET runtime, that you have the latest Windows service packs and updates… if it still doesn’t work I’m afraid I can’t help, sorry.

    Quite right that FxCop and Reflector are both great tools though. There are others for XNA and .NET, which I’ll be covering soon hopefully.

    Comment by bittermanandy — August 17, 2008 @ 11:13 pm | Reply

  4. Oh – I nearly forgot to mention. Since spending hours trying to figure out how to write my own FxCop rules, I found a site that explains everything much more clearly than the sites I linked to above, and which would have made everything much easier if I’d found it earlier, at http://www.binarycoder.net/fxcop/html/index.html. It’s still not for the faint-hearted, and I reckon most people would be better off spending their time actually making games (just sticking to the basic FxCop rules), but if you can justify the time and effort (pro studios probably could for example) or just fancy having a go, that site is the best resource I’ve found.

    Comment by bittermanandy — August 17, 2008 @ 11:25 pm | Reply

  5. Um, Yes I have heard a lot about Reflector and for what it does I’m sure it does it extremely well. I’ve just not needed to peek into any assemblies that I do not “own” thus far in my C# coding experience.

    Somehow, after saying that, I feel that I am missing out on something. Perhaps it is time to pry!?

    Comment by Antony Kancidrowski — August 18, 2008 @ 1:30 am | Reply

  6. Well, granted it’s not something I use daily or anything. But there are times, like the one I described, where it is invaluable. It’s also helpful for understanding where garbage is getting produced, it’s a good resource for beginners in particular to see some “real” code (algorithmically anyway – it’s not the actual code as was written), and sometimes it’s just interesting to see how particular problems were solved.

    Comment by bittermanandy — August 18, 2008 @ 8:09 am | Reply

  7. Aha, got it working! It’s definitely interesting to see (approximately) how the system and XNA libraries you’re using solve some of their problems, like intersections of 3D objects, etc. Also just kind of fun for the game-programming geek/aficionado. oh, and as for the problem: you can’t take a peek at the Xbox 360 DLLs. I was getting the various .NET libraries fine, etc, but any of the Xbox XNA DLLs wouldn’t disassemble properly. I’m now just taking a peek into the Windows version of XNA. Still very useful and informative stuff. Thanks for pointing out all these interesting and productive tools!

    Comment by Kolo Rahl — August 18, 2008 @ 11:28 pm | Reply

  8. […] instead – though you can still get caught out if you’re not careful, as my previous post on Reflector […]

    Pingback by Garbage, Part Two (Director’s Cut): oh, alright then « Pandemonium — November 12, 2008 @ 12:27 am | Reply

  9. @Kolo:
    Thanks for that. I was having the same issue and I couldn’t figure it out — it made no sense that I couldn’t reflect into the XNA library! I’ve been searching for almost half an hour trying to figure it out. What a stupid thing to overlook, haha. Thanks again!

    Comment by mgriffie — June 12, 2009 @ 2:56 am | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: