Converting between VB and C#, and .NET Decompilation

Here is post to follow up on the

comment

on converting between C# and VB code by
Rod Howarth
on the post on

Application Events in VB
.

First of all, converting code between C# and VB is possible and can be done automatically. C# and VB.NET both produce the identical intermediate language IL code, which is what is actually stored in the .NET assembly and evaluated by the .NET framework. The IL code contains all information required to regenerate the source code. So, on one hand, you can translate between C# and VB. Furthermore, you can even recreate source code in any language of your choice by decompiling the IL code in the assembly.

To translate between C# and VB, just google for “c# vb translator”. Rod mentioned a couple of such translators; he says:

Also useful for anyone who is a VB coder and wants to convert samples are the many VB to C# code converters available. Using a Google query such as

this one

will turn up tools such as

http://www.developerfusion.com/tools/convert/csharp-to-vb

and

http://www.carlosag.net/Tools/CodeTranslator
.
They may not do whole projects but they can help you decipher the samples!

To analyse or decompile any .NET assembly, even without access to the source code, a tool that I use is
Reflector
by
Lutz Roeder.

It includes reverse engineering functionality, for instance a decompilation and source code generation feature, so it can read your compiled DLL and decompile the IL intermediate language it contains into various languages including C#, VB, managed C++, and others. It can obviously also be used to list all namespaces and classes defined in an assembly, and their methods and properties.

Here is an example of decompiling a Revit external command assembly I built using C# and requesting VB code for it:

Reflector

See the VB code? I wrote this application in C#! My version of Reflector will also decompile into Delphi, Chrome and managed C++, besides C# and VB.

Although … please note that even Reflector is not always 100% correct … if you look carefully at the screen snapshot, note that it seems to have come up with something strange; in the line following

If (Not Nothing Is PlanarFace) Then

it says

Dim 8__locals2 As c__DisplayClass1

That will probably not compile correctly. Thanks to Adam Nagy, a DevTech colleague of mine, for noticing this error.

I find this an invaluable tool, and often use it to explore my own .NET assemblies, just to ensure that they really do contain what I am expecting them to.


Comments

6 responses to “Converting between VB and C#, and .NET Decompilation”

  1. Yeah good info, but please tell me HOW TO use reflector. I ran my exe and I get little box with no way to do anything. It does provide me with nice but ugly unsolvable errors that say “The remote name could not be resolved: ‘reflector.re-gate.com’”. I’m not trying to go anywhere remote. The app is local. Do I access it through Visual Studio?

  2. Hi Jeremy,
    there is another good tool, http://www.icsharpcode.net/OpenSource/SD/
    a open source ide for .Net. This also can convert from c# to vb.
    And it supports debugging of assemblies like the one for Revit, when you have to start an external program. The express editions from visual studio can do this out of the box.
    Cheers,
    Tim

  3. Hi Tim,
    Thank you for the valuable pointer. Do you actually mean “cannot do this out of the box”? However, there is a trick to debug in the express addition as well, some small twist in the xml project file, I think, documented in Kean’s blog, I think, isn’t there?
    Cheers, Jeremy

  4. Hi Eje,
    I simply start up Reflector, for instance by typing C:\bin\reflector.exe on the command line, and then open an assembly file by selecting File > Open.
    Cheers, Jeremy

  5. Hi Jeremy,
    I followed this guide http://usa.autodesk.com/adsk/servlet/index?siteID=123112&id=20132893 to debug in express.
    btw, reflector is now owned by redgate. Are you still using Lutz version or the redgate version?
    regards, Rey

  6. Dear Rey,
    Thank you for a relevant question.
    I use very little commercial software, actually, but in this case I did go and get the updated version.
    I am currently using .NET Reflector 6.
    Cheers, Jeremy.

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading