Leo Leo - 1 year ago 139
Vb.net Question

Roslyn: Convert C# to VB

I have the case that I need to convert a C#- to a VB.NET project. (I want to automate this, so I cannot use an online tool or something similar)

There is a "Paste as C#/VB" sample visual studio extension which seemed to be able to do this.

I tried converting this class:

namespace TestApplication
class Class1
/// <summary>
/// Lorem
/// </summary>
public void Lorem()


But it ended up with this:

Namespace TestApplication

Class Class1

''' <summary> Lorem </summary> Public Sub Lorem()
End Sub
End Class
End Namespace

It does not only happen when XML-documentation comments are provided, but sometimes also when inheriting other classes etc.

Here's the code that handles the convertion of the sample:

csharpToVisualBasicConverter.Convert returns a SyntaxNode instance.

private void PasteAsVB()
var csharpCode = Clipboard.GetText(TextDataFormat.Text);

var tree = CS.SyntaxTree.ParseText(csharpCode);
var visualBasicCode = csharpToVisualBasicConverter.Convert(tree);

var start = wpfTextView.Selection.SelectedSpans.Min(s => s.Start).Position;
var end = wpfTextView.Selection.SelectedSpans.Max(s => s.End).Position;
var span = Span.FromBounds(start, end);

wpfTextView.TextBuffer.Replace(span, visualBasicCode.ToFullString());

As there is no exception when calling the convert method, I assume the method returns a valid SyntaxNode and the SyntaxNode.ToFullString() method or an encoding issue messes up the line breaks etc.

Did anybody experience this issue before and found a solution?

Answer Source

I developed an application 7 years ago in VB.NET and I had to integrate a component into it whose SDK was written in C# only. The application that I had to integrate was reasonably large amd complex. I used this product to convert the C# to VB.NET and whilst the finished product did require some tweaking and some thorough testing, I don't recall the process being particularly harrowing. The outcome was excellent. The application worked well and it is still going strong today.


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download