.NET Language Conversions


Since .NET has been out, I have seen quite a few people asking about tools to convert from one .NET language to another. Just recently, I saw an offer for VB Conversions, which made me decide to post about your options if you need to covert languages. Once I get a copy of VB Conversions, I will give you my feelings about the product.
 

Option 1: Compile to an Assembly

This is not a conversion option, per se, but it is one of the best ways to avoid having to convert code. If the code you have in another language is in classes, this is quite easy to accomplish. Simply move the classes to a class library project and compile. You then can avoid having to convert languages, as the compiled assembly can be used from any .NET language. There are some caveats here, as C# can produce code that is unusuable from VB. As an example, consider the following class (no, I would never do this in real life):

namespace Test
{
   
public class Test
    {
       
public int UserId { get; set; }
       
public string userId { get; set; }
    }
}

This is perfectly legal in C#, as far as the compiler is concerned, but makes it completely unuseable from Visual Basic.

Option 2: Reverse Engineer

I am still in awe that there are tons of developers who do not know about Lutz Roeder’s Reflector tool. It is simply one of the most useful tools for .NET coding. If you combine it with Denis Bauer’s File Dissambler you have a nice quite to fully reverse engineer .NET code. It does not work well on properly obfuscated code, of course, but I am assuming you want to convert code you have source for to another language.
 
Here is what you do:
  1. Open the source you want to convert
  2. Compile it to an assembly
  3. Open the assembly in Reflector
  4. Open File Dissasembler from the Tools menu
  5. Dissasemble the source

If you merely want to copy and paste a section, you can do that in Reflector without any plug ins.

Does it work? Yes. Here is an example of a class from a new project:

[Serializable]
public partial class SimCollection : RepositoryList<Sim, SimCollection>
{       
     
public SimCollection() {}
   

    public SimCollection Filter()
    {
       
for (int i = this.Count – 1; i > -1; i–)
        {
           
Sim o = this[i];
           
foreach (SubSonic.Where w in this.wheres)
            {
               
bool remove = false;
                System.Reflection.
PropertyInfo pi = o.GetType().GetProperty(w.ColumnName);
               
if (pi.CanRead)
                {
                   
object val = pi.GetValue(o, null);
                   
switch (w.Comparison)
                    {
                       
case SubSonic.Comparison.Equals:
                           
if (!val.Equals(w.ParameterValue))
                            {
                                remove =
true;
                            }
                           
break;
                    }
                }
               
if (remove)
                {
                   
this.Remove(o);
                   
break;
                }
            }
        }
       
return this;
    }
}

and here is the resulting VB code:

<Serializable> _
Public Class SimCollection
    Inherits RepositoryList(Of Sim, SimCollection)
   
‘ Methods
    Public Function Filter() As SimCollection
        Dim i As Integer = (MyBase.Count1)
       
Do While (i > -1)
           
Dim o As Sim = MyBase.Item(i)
           
Dim w As Where
            For Each w In MyBase.wheres
                Dim remove As Boolean = False
                Dim pi As PropertyInfo = o.GetType.GetProperty(w.ColumnName)
               
If pi.CanRead Then
                    Dim val As Object = pi.GetValue(o, Nothing)
                   
If ((w.Comparison = Comparison.Equals) AndAlso Not val.Equals(w.ParameterValue)) Then
                        remove = True
                    End If
                End If
                If remove Then
                    MyBase.Remove(o)
                   
Exit For
                End If
            Next
            i -= 1
        Loop
        Return Me
    End Function
End Class

All nicely packaged up. Now, Reflector is a bit old for .NET 3.5, so you may hit some hiccoughs. I have also noticed that you occasionally have to clean up some code, as the names created are bogus. But, I have even reversed some code that was obfuscated by a weak obfuscator and got it to compile. This is a good reason to spend a bit and buy a good obfuscator.

Option 3: Tools

I mention VB Converter, which I cannot recommend until I get the opportunity to review it. There is also converters from Tangible Software. I have not tried them either, but may ask for an NFR so I can play. After all, it is only fair if I am going to review one to review others.

For C# to VB, for example, there is an online tool:

http://labs.developerfusion.co.uk/convert/csharp-to-vb.aspx

Here is the code it delivered for the same project as above:

1.  <Serializable()> _

2.  Public Partial Class SimCollection

3.      Inherits RepositoryList(Of Sim, SimCollection)

4.      Public Sub New()

5.      End Sub

6.     

7.      ”’ <summary>

8.      ”’ Filters an existing collection based on the set criteria. This is an in-memory filter

9.      ”’ Thanks to developingchris for this!

10.     ”’ </summary>

11.     ”’ <returns>SimCollection</returns>

12.     Public Function Filter() As SimCollection

13.         For i As Integer = Me.Count – 1 To -1 + 1 Step -1

14.             Dim o As Sim = Me(i)

15.             For Each w As SubSonic.Where In Me.wheres

16.                 Dim remove As Boolean = False

17.                 Dim pi As System.Reflection.PropertyInfo = o.[GetType]().GetProperty(w.ColumnName)

18.                 If pi.CanRead Then

19.                     Dim val As Object = pi.GetValue(o, Nothing)

20.                     Select Case w.Comparison

21.                         Case SubSonic.Comparison.Equals

22.                             If Not val.Equals(w.ParameterValue) Then

23.                                 remove = True

24.                             End If

25.                             Exit Select

26.                     End Select

27.                 End If

28.                 If remove Then

29.                     Me.Remove(o)

30.                     Exit For

31.                 End If

32.             Next

33.         Next

34.         Return Me

35.     End Function

36. End Class

Makes me wonder if the Reflector DLLs are being used underneath the hood. Any ideas? Sarcastic

Another is http://www.carlosag.net/Tools/CodeTranslator/default.aspx, but I found it failed on my code. http://authors.aspalliance.com/aldotnet/examples/translate.aspx did better, but it fails on partial classes, letting me know it is dated.

If you are still in Vsiual Studio 2005, you can also examine the paste as Visual Basic plug in. It has not been updated for VS 2008, however. I have not run it through its paces, as I am a VB to C# person. Angel

There are fewer tools for VB to C#. This may be biased due to the fact that C# developers know VB, or are assumed to know VB, etc. I find this to work as a generality, at least from my experience, but I am sure there are some C# developers who want to convert some VB code.

Coming Up

I will get a chance to take VB Conversions around the block (have my copy already) and let you know. I have also written an email to Tangible and will see if they would like me to see how their tool does, as well.

Peace and Grace,
Greg 

Advertisements

2 Responses to .NET Language Conversions

  1. Kelly says:

    I will be looking forward to your review of this product.

  2. Unknown says:

    thanks for the attribution on the filter method.Is that in subsonic trunk now?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: