The Zen of Volta


This article was originally published by developmentor

Refactoring is a common programmer technique to cleanup and reorganize source code to add new features or make it more maintainable. Volta extends this concept to the compiled code as well by taking the application and changing the code to add new functionality or change the architecture into multiple tiers. This type of IL re-writing has the most potential for making a big impact on the .NET development world today. The Volta team showcased two examples of refactoring: Async and TierSplitting.

Async Refactoring

Async rewriting allows a developer to write a normal, synchronous method and then declare a method signature for the asynchronous version with no implementation. They can then annotate that declaration with the [Async] attribute and Volta will generate the implementation for it automatically. For example, imagine we had a class whose job it was to take a generated log file and reformat it to XML. For a large file this may be a time consuming task so it would be good to be able to run this asynchronously.

With Volta, we can focus on the basic functionality of converting the logfile and not worry about the asynchronous version at all:

class LogfileReformatter {
    public string Reformat(string logFilename){
        string newFilename = null;
        using (FileStream fs = File.OpenRead(logFileNames)) {
            // reformat log file contents
        return newFilename;

    public extern void Reformat(string logFilename, Callback<string&rt; callback);

When rewriting the IL, the Volta compiler sees the [Async] attribute and generates the actual code for making the method asynchronous (taken from Reflector):

public void Reformat(string logFilename, Callback callback) {
    new __ReformatAsyncHelper(
        new __ReformatAsyncHelper.SyncDelegate(this.Reformat),

So what is this code actually doing? First, it creates a new object passing in a delegate that is wrapping the synchronous version of the Reformat method and the Callback. Next, it calls Invoke on the new object – let’s have a look at what Invoke does:

public void invoke(string logFilename) {
                                new AsyncCallback(this.Handle),null);

This should look pretty familiar to most .NET developers – it is simply calling the SyncDelegate instance asynchronously passing a method called Handle that will be called when the method completes. Let’s finally have a look at the Handle method:

private void Handle(IAsyncResult result) {

is called on the SyncDelegate instance to get the result of the synchronous call and then the callback is fired passing this result.

So although Volta isn’t generating particularly complex code, it is allowing the developer to concentrate on the business problem at hand and indicate a desire for the operation to support asynchronous execution. Volta takes care of providing the actual implementation of the asynchronous version.

You might also like...


About the author

Richard Blewett United Kingdom

Richard is the CTO of DevelopMentor UK.

He began life as a mainframe programmer writing ALGOL and COBOL, but jumped to OS/2 after a year. In 1995, he started developing under Windows an...

Interested in writing for us? Find out more.


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Weeks of coding can save you hours of planning.”