views:

900

answers:

5

I hope this isn't considered a duplicate since it's more pointed than similar questions (I'm curious about a specific weakness in C# to VB.net conversion utilities).

I've been looking at using a tool like this .net code converter to convert a class library to VB since I'm the only one in my group comfortable with C#. The problem I've run into is that it doesn't generate proper VB for automatic properties. It creates empty get/set routines.

So this:

public string TransactionType { get; private set; }

Becomes this:

Public Property TransactionType() As String
    Get
    End Get
    Private Set(ByVal value As String)
    End Set
End Property

The tools linked here and here have similar issues - some create valid properties, but they don't respect the access level of the set routine.

Side question - If you were going to fix the converter on DeveloperFusion, would you have it return something like this?

Private _TransactionType As String
Public Property TransactionType() As String
    Get
        Return _TransactionType
    End Get
    Private Set(ByVal value As String)
        _TransactionType = value
    End Set
End Property
+2  A: 

I'd recommend compiling the code and using something like Red-Gate's reflector http://www.red-gate.com/products/reflector/index.htm to handle the conversion. Now it's not "perfect," and I'm not sure if it handles automatic properties (though I'd imagine it would).

What makes this possible is that when you compile .NET language down to IL they're exactly the same. The language is just another layer on top of that. So 2 properties that would look at the same in their native languages compile to the exact same IL code. So reversing this to other languages using something like Reflector is easy and quick.

Chad Moran
+1  A: 

As an answer to your side question: yes, that code is pretty much exactly what I'd get it to produce. You can't get it to do exactly what the C# code does, which is to make the name of the variable "unspeakable" (i.e. impossible to reference in code) but that's probably close enough.

Jon Skeet
+1  A: 

I would suggest checking out SharpDevelop (sometimes written as #develop). It's an Open Source .NET IDE that, among other things, can convert (with some issues) from C# to VB.NET and vice versa.

theraccoonbear
+2  A: 

I stumbled on this while looking for a way to automate using reflector to translate code (since there are several plugins for it to generate code in other languages (even PowerShell)), but you made me wonder, so I tried it. With the compatibility set to .Net 3.5, it converts your example to this:

Property TransactionType As String
  Public Get
  Private Set(ByVal value As String)
End Property

If you dig in, it does report that there are compiler generated methods which it doesn't export in VB.Net or C# with 3.5 compatability on ... HOWEVER, if you switch it to 2.0, the same code will generate this:

Property TransactionType As String
    Public Get
        Return Me.<TransactionType>k__BackingField
    End Get
    Private Set(ByVal value As String)
        Me.<TransactionType>k__BackingField = value
    End Set
End Property

<CompilerGenerated> _
Private <TransactionType>k__BackingField As String

P.S.: if you try using a disassembler like Reflector to generate code, remember to keep the .pdb file around so you get proper names for the variables ;)

Jaykul
Thanks for the tip about the .pdb file and Reflector. I did not know that.
Steven Murawski
+3  A: 

We've now updated the code generator to support this scenario. If you spot any others that we're not doing very well, please do drop me a line.

James Crowley
Much more than I expected. Thanks.
Peter LaComb Jr.