Monday, September 03, 2007 12:00 AM bart

Visual Basic 9.0 Feature Focus - Runtime Agility

Welcome back to the Visual Basic 9.0 Feature Focus blog series. In this post, we'll cover Runtime Agility. Before we continue, I have to warn you: this is a HUGE feature even though it hasn't been covered that much yet in the blogosphere. Historically, Visual Basic has been a language that's highly dependent on a runtime library. People from the old VB days will certainly remember msvbvmxx.dll, the Microsoft Visual Basic Virtual Machine version xx. Not only did this library contain runtime functionality (e.g. for memory management), it also did contain helper functions that are used to give operations in Visual Basic the right semantics (e.g. how a string compare works in VB). As we moved into the .NET era, the need popped up to keep the semantics consistent compared to the past. That's why Visual Basic .NET requires a runtime library called Microsoft.VisualBasic.dll which can be found in the GAC. So far so good, but what about the implications of this? Right, every VB.NET application has a dependency on this runtime library, which implies the library has to be redistributed and loaded and maybe there's lots of stuff inside Microsoft.VisualBasic.dll that your app is never going to use. Sol, what about eliminating this dependency? Enter runtime agility!

Let's start to outline the current situation with a simple sample. Compile the following using the VB 8.0 compiler (either through the IDE or using vbc.exe:

Class Program
   Shared Sub Main()
   End Sub
End Class

Now, when you take a look at the generated IL code metadata using ILDASM.exe you'll see the following:


Even for an application as easy (that's an understatement) as the above, the Microsoft.VisualBasic assembly is referenced by the compiler. Now in Visual Basic 9.0, things are changing, also because of the growing need for a lightweight runtime (e.g. in the context of Silverlight). Now it's possible to compile applications without the Visual Basic runtime or with a different runtime. Using the Visual Basic 9.0 compiler, compile the code above with the following command:

vbc -vbruntime- demo.vb

This will produce the following metadata:


As you can see, there's no dependency on the Microsoft.VisualBasic assembly anymore. But what if you start to use functionality that requires the runtime library, like this:

Class Program
   Shared Sub Main(ByVal args As String())
      Console.WriteLine(args(0) = args(1))
   End Sub
End Class

In here, we're performing a string comparison, which gets translated into a CompareString method call, a function that's defined inside the Microsoft.VisualBasic assembly:


So, if you try to compile this without the VB runtime, you'd end up with the following compile error message:


The cool thing is you can build your own runtime library that just has the functionality you need. For example, I'll be using C# to write a small VB Runtime library that just has a CompareString operator:

using System;

namespace Microsoft.VisualBasic.CompilerServices
   public class Operators
      public static int CompareString(string left, string right, bool textCompare)
         return 0;

The code above is for demo purposes only: it will say the strings are equal no matter what the input is (0 = equal, negative = left < right, positive = left > right). Compile this library using:

csc -t:library vbruntime.cs

Alternative you could write the runtime implementation in VB itself too:

Namespace Microsoft.VisualBasic.CompilerServices
   Public Class Operators
      Public Shared Function CompareString(ByVal left As String, ByVal right As String, ByVal textCompare As Boolean) As Integer
         Return 0
      End Function
   End Class
End Namespace

which can be compiled using:

vbc -vbruntime- -t:library vbruntime.vb

Finally, compile the Visual Basic demo code using:

vbc -vbruntime:vbruntime.dll demo.vb

Now, everything should work fine and you should see the following in the metadata of the generated assembly:


And in the Main function, our CompareString operation is used:


When you execute the program, you'll get True now matter what the two input arguments are (see our custom 'bad' implementation):

>demo Bart John

Tip: Try to compile an average-sized application using the -vbruntime- flag to see how much runtime dependencies there are.

Happy coding! | Digg It | Technorati | Blinklist | Furl | reddit | DotNetKicks

Filed under:


# 7 Links Today (2007-09-04)

Tuesday, September 04, 2007 8:20 AM by 7 Links Today (2007-09-04)

Pingback from  7 Links Today (2007-09-04)

# VB 9.0 Feature Focus

Wednesday, September 05, 2007 6:50 PM by Nick's .NET Travels

In my previous post I commented that VB is coming of age in Visual Studio 2008 with better support for

# New Features of Visual Basic 9 Article Series

Thursday, September 27, 2007 5:47 AM by Walter Stiers - Academic Relations Team (BeLux)

In a series of 15 posts, Bart De Smet explores several of the new features in Visual Basic 9 . These

# Awesome VB9 Blog Series by Bart De Smet (Amanda Silver)

Wednesday, November 21, 2007 2:48 AM by The Visual Basic Team

I guess I've been working too hard on getting the product ready to ship to notice that Bart has put together

# Awesome VB9 Blog Series by Bart De Smet (Amanda Silver)

Wednesday, November 21, 2007 3:00 AM by Noticias externas

I guess I&#39;ve been working too hard on getting the product ready to ship to notice that Bart has put

# MSDN Blog Postings &raquo; Awesome VB9 Blog Series by Bart De Smet (Amanda Silver)

Pingback from  MSDN Blog Postings  &raquo; Awesome VB9 Blog Series by Bart De Smet (Amanda Silver)

# Visual Basic 9.0 Feature Focus - Runtime Agility

Wednesday, December 05, 2007 1:40 PM by Visual Basic 9.0 Feature Focus - Runtime Agility

Pingback from  Visual Basic 9.0 Feature Focus - Runtime Agility


# VB 9.0 Feature Focus – Link Collection

Saturday, August 09, 2008 7:17 AM by B# .NET Blog

Collecting a few of my posts for easy quick reference: Visual Basic 9.0 Feature Focus – Introduction

# Expression Tree Normalization – How Many Ways To Say “String == String”?

Wednesday, August 13, 2008 10:06 AM by B# .NET Blog

Recently I gave a talk at TechEd South Africa about Custom LINQ Providers. Unfortunately the session

# official dragon pharma

Thursday, March 23, 2017 9:04 PM by official dragon pharma

Pingback from  official dragon pharma