Los Techies : Blogs about software and anything tech!

Fun with Mono.Cecil


During the past couple of days, I took it upon myself to automatically generate documentation about our "Scriptable" methods. These methods are used to provide an entry point into our Silverlight application for our automated acceptance test suite. Since we do not run our tests from the browser, which is currently the only way to go with an out-of-box tool, we decided to write our tests with RSpec, in Ruby, by using Watir to start up a web browser and connect to our application. Since Silverlight is inaccessible through the DOM, our scriptable methods are the only way we have access to our controls. I've talked about this whole process in the past.

Basically, a scriptable method is a method that has a ScriptableMemberAttribute on it. The Silverlight plugin takes those methods and makes them accessible through Javascript, which is how we can call them from our Ruby test suite. Most of our views and controls have a bunch of of those and things have gotten a bit out of hand. One of the things that really slows us down while writing tests is just figuring out how to get access to a certain control. For example, starting from the main view, what is the path of scriptable methods that you must take to get to the latest error message? It might be: Main View -> Sidebar Control -> Menu Item #6 -> Error Message Panel. If you don't know your way, you have to set breakpoints in your code and manually, through trial and error, figure out how to get to where you want to go.

Enough! Now, we have a crude, basic, version 1 Html Scriptable Method Generator 3000 (tm)!

First thing that had to be done was load up our web client assembly and reflect on it. I quickly hit a wall when the Microsoft reflection utilities would refuse to load it up. I realized that it was a Silverlight library that I was trying to load and that I would have to create a Silverlight solution instead of a regular, normal CLR solution. You see, there is a difference between the two .Net runtimes. A normal CLR application cannot reflect upon a Silverlight library, which is compiled using the Core CLR. (I also wrote about this in the past)

Darn!

Thankfully, however, somebody suggested on stackoverflow.com to use Mono.Cecil instead.

Mono.Cecil is a library that allows you to reflect on assemblies, basically. But it can do much more. As my colleague Gabriel has blogged about, it can allow you to inject CIL code into a compile library. It will even let you inspect all you anonymous methods in your code. Is it hard to use? Nope! Just download the dll, add it to your project references and you're good to go. Windows or Linux, Monodevelop or Visual Studio; it just works (tm).

Documentation is spread out a bit everywhere, but I found this to be really helpful to start with: the Mono.Cecil FAQ. Additionally, Google will help you find a bunch of usage examples. I would post some code, except that I didn't do anything that you can't find in the FAQ that I linked to above. The scriptable doc generator loops over all the methods that have a ScriptableMemberAttribute on them, using the main view as a starting point, and generates a tree where each node is a scriptable method, it's return type and a sub-tree of child scriptable methods. Using that tree, the generator creates an Html file for each class that has scriptable methods and lists all of those methods in an unordered list.

I plan on making this better in the future by adding links and collapsible tree views. It'll also be very cool to add it to our continuous integration system to update a wiki site whenever a build is finished. And ultimately, I hope this makes us much, much faster in writing our tests. Eliminating waste is always the ultimate goal.

 

Kick It on DotNetKicks.com
Posted Nov 25 2009, 05:35 PM by Louis Salin

Comments

Fun with Mono.Cecil - Louis Salin's Blog - Los Techies : Blogs … Silverlight Blog wrote Fun with Mono.Cecil - Louis Salin's Blog - Los Techies : Blogs … Silverlight Blog
on 11-25-2009 11:37 PM

Pingback from  Fun with Mono.Cecil - Louis Salin's Blog - Los Techies : Blogs … Silverlight Blog

Jb Evain wrote re: Fun with Mono.Cecil
on 11-26-2009 4:46 AM

Hey Louis,

Good luck with your exploration!

Pay us a visit on the mono-cecil google group should you have a question!

Best,

Ryan Svihla wrote re: Fun with Mono.Cecil
on 11-27-2009 9:24 AM

I need to pick up cecil into my reflection skillset, I really hate reflection in .Net (still better than Java's), and anything I can do to inspect class metadata without having to pass null into random args would be awesome.

Louis Salin wrote re: Fun with Mono.Cecil
on 11-27-2009 1:21 PM

@Jb, thanks! So far, I'm amazed how easy you guys are making things for .Net in general!

@Ryan, be sure to read Gabriel's blog. I've link to it in this post. He's using Cecil to inject code into our web client dll to make his own profiler. Good stuff!

uaeccbodomr wrote re: Fun with Mono.Cecil
on 12-04-2009 11:48 AM

k8O3nz  <a href="gstseqhehffv.com/.../a>, [url=http://onfoyiiwliqf.com/]onfoyiiwliqf[/url], [link=http://ygrrwbafndjd.com/]ygrrwbafndjd[/link], http://wlbrxbuoxdhw.com/

nxlavdqql wrote re: Fun with Mono.Cecil
on 12-04-2009 12:38 PM

Lf6jSV  <a href="udbbzoxwutsv.com/.../a>, [url=http://lvxgzvmkvrxo.com/]lvxgzvmkvrxo[/url], [link=http://nryudksfdzqw.com/]nryudksfdzqw[/link], http://xppibkxpishk.com/

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

Enter the numbers above:
Copyright Los Techies 2008, 2009. All rights reserved.
Powered by Community Server (Commercial Edition), by Telligent Systems