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
True
Tip: Try to compile an average-sized application using the -vbruntime- flag to see how much runtime dependencies there are.
Happy coding!
Del.icio.us |
Digg It |
Technorati |
Blinklist |
Furl |
reddit |
DotNetKicks
Filed under: VB 9.0