Skip to main content

Decompiling a VB.NET assembly to C#

Updated 9 min readMarkdown

You inherit a .dll. It is part of a line-of-business app that has run for fifteen years, the source was last seen on a developer's machine that was decommissioned a decade ago, and the only clue to its origin is that everyone who remembers it calls it "the VB component." You open it in a decompiler expecting Visual Basic, and out comes C# — C# littered with calls to types you have never written: NewLateBinding.LateGet, Operators.CompareString, Conversions.ToDouble, ProjectData.SetProjectError.

That is not the decompiler guessing wrong. It is the decompiler being honest. The CLR has no idea your assembly was once Visual Basic — metadata and IL carry no source-language tag — but the VB compiler left fingerprints everywhere, and those fingerprints are those helper calls. This post walks through what the Visual Basic compiler did to your code on its way to IL, and how Glass.NET — the free, static-only .NET decompiler — turns a binary that was never C# into C# you can actually read.

Metadata has no language; the compiler has an accent

C# and Visual Basic both target the same CLR. By the time MSBuild is done, a VB project and a C# project produce the same kind of PE file: a type system described in metadata tables, methods described in IL. There is no "this was VB" flag. A decompiler therefore always reconstructs C#, because C# is the language that maps most directly onto the metadata — and that is exactly what you want when the goal is to read the thing.

But each compiler has an accent. The VB compiler reaches for the Visual Basic runtime, Microsoft.VisualBasic.dll, to implement language features the CLR does not provide natively. Learn to hear the accent and the decompiled C# stops looking strange and starts telling you precisely what the original VB said.

Modules become sealed static classes

A VB Module has no direct C# equivalent, so the compiler lowers it into a sealed class whose members are all static, tagged with a marker attribute. This VB:

Module Billing
    Public Function NetTotal(gross As Double, taxRate As Double) As Double
        Return gross / (1 + taxRate)
    End Function
End Module

compiles to a type carrying Microsoft.VisualBasic.CompilerServices.StandardModuleAttribute, with the method emitted as:

.class public auto ansi sealed Billing
    extends [System.Runtime]System.Object
{
  .custom instance void [Microsoft.VisualBasic]...StandardModuleAttribute::.ctor()
  .method public static float64 NetTotal(float64 gross, float64 taxRate) ...
}

Glass recognises the StandardModule marker and the all-static shape and reconstructs the idiomatic C# equivalent — a static class — rather than an abstract-sealed oddity:

[StandardModule]
internal static class Billing
{
    public static double NetTotal(double gross, double taxRate)
        => gross / (1.0 + taxRate);
}

The [StandardModule] attribute is left visible deliberately: it is the single clearest fingerprint that the original was a VB Module, and hiding it would erase information you may need.

Late binding: the Option Strict Off tax

Nothing marks a VB assembly like late binding. Under the default Option Strict Off, this is perfectly legal VB:

Public Function TitleOf(doc As Object) As String
    Return doc.Metadata.Title
End Function

doc is typed Object, and .Metadata.Title is resolved at runtime by reflection. C# would need dynamic for this — but the VB compiler does not emit a dynamic call site. It emits a call into the Visual Basic runtime's late binder. The IL for doc.Metadata.Title becomes two NewLateBinding.LateGet calls, and Glass reconstructs them faithfully:

public static string TitleOf(object doc)
{
    object metadata = NewLateBinding.LateGet(
        doc, null, "Metadata", new object[0], null, null, null);
    return Conversions.ToString(
        NewLateBinding.LateGet(
            metadata, null, "Title", new object[0], null, null, null));
}

That is verbose, but it is the truth: every member access on a late-bound Object was a reflective lookup, and the decompiled C# shows exactly that. When you see NewLateBinding.LateGet or LateCall, you are reading code that was written with Option Strict Off. Mentally, LateGet(x, null, "P", …) is just ((dynamic)x).P — Glass gives you a menu option to collapse recognised late-binding calls into dynamic member access when you would rather read intent than mechanism.

String comparison goes through Operators.CompareString

In C#, a == b on two strings is an ordinal-ish string.Equals. In VB, the = operator on strings depends on Option Compare, and the compiler never emits a direct comparison. It emits Operators.CompareString, which takes a third argument — TextCompare — that is false for Option Compare Binary and true for Option Compare Text (case-insensitive, culture-aware). So this VB:

If status = "Active" Then Return True

decompiles to:

if (Operators.CompareString(status, "Active", TextCompare: false) == 0)
    return true;

The third argument is diagnostic gold. TextCompare: true means the whole module was compiled with Option Compare Text, and every string = in it was a case-insensitive compare — a behaviour you would badly want to preserve if you ever port this code to C#, where == would silently become case-sensitive. Glass surfaces the flag rather than dropping it.

On Error becomes ProjectData bookkeeping

VB's unstructured error handling — On Error Resume Next, On Error GoTo — has no IL instruction behind it. The compiler synthesises a state machine around the Visual Basic runtime's ProjectData error registry. A method with On Error Resume Next decompiles into a try/catch skeleton with ProjectData.SetProjectError(ex) in the catch and ProjectData.ClearProjectError() where control resumes, plus a hidden integer tracking the "resume" target. Glass reconstructs the control flow accurately — you can see which statements were protected and where execution was meant to resume — even though the original On Error keyword is gone. Seeing ProjectData.SetProjectError in a catch block is a dead giveaway that the source used On Error, not Try/Catch.

The figure below summarises which VB accent maps to which runtime fingerprint, and what Glass shows you for each.

VB featureruntime fingerprintGlass shows C#ModuleStandardModuleAttributestatic classlate bindingNewLateBinding.LateGetLateGet(...) / dynamicOption CompareOperators.CompareStringCompareString(...) == 0On Error Resume NextProjectData.SetProjectErrortry / catch skeletonimplicit conversionConversions.ToDoubleConversions.ToDouble(x)

Implicit conversions and the My namespace

Two smaller fingerprints round out the picture. First, VB converts implicitly where C# refuses. A VB expression that assigns a String to a Double compiles to Conversions.ToDouble(s) — the Visual Basic runtime's lenient parser — not a cast, and Glass shows that call so you know a parse, not a cast, was happening. Second, the My feature (My.Application, My.Computer, My.Settings) is pure compiler magic: the VB compiler generates a hidden MyProject type with nested MyApplication, MyComputer and settings classes, and rewrites every My. access into a property on it. In the decompiled C# you will find a generated MyProject class you never wrote — that is where My went.

Reading it with Glass

Open the assembly in the Glass GUI and browse the tree; VB modules show up as the static classes they became, and the Visual Basic runtime reference is listed under the assembly's dependencies — itself a one-glance confirmation the binary was VB. To pull a single member from the command line:

glass decompile Legacy.Billing.dll --type Billing --member NetTotal

Add --il to see the raw IL beside the reconstructed C#, which is the fastest way to confirm what a LateGet or CompareString call was doing in the original:

glass decompile Legacy.Billing.dll --type Billing --member TitleOf --il

When you are ready to work on the whole thing — grep it, point an analyzer at it, or keep it as a permanent reference — export it to a buildable C# project:

glass export Legacy.Billing.dll --output ./Legacy.Billing.Source

You get a .csproj, reconstructed .cs files and any extracted resources. Opened in your editor, a fifteen-year-old VB component is now a C# project you can read, search and reason about — the first real step in either keeping it alive or porting it forward.

Glass is strictly static: it reads metadata, IL and resource blobs and reconstructs source, and never executes the assembly. That makes inspecting your own orphaned VB component, a licensed third-party library, or code you are analysing for interoperability both safe and sound — and Glass is completely free, with no licence, no seats and no sign-up.

No comments yet. Be the first to comment.