Roslyn: Convert C# to VB

2020-07-18 02:56发布

问题:

I have the case wherein 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 find a solution?

回答1:

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.

http://www.tangiblesoftwaresolutions.com/Product_Details/Instant_VB.html



回答2:

FYI there is now a Roslyn Code Converter add-in for Visual Studio 2015.2+:

https://marketplace.visualstudio.com/items?itemName=SharpDevelopTeam.CodeConverter

The web version is here and the repo is here.



回答3:

SharpDevelop has a menu doing that conversion for the entire project. You can automate it interacting with the GUI (simulating mouse clicks) or, since it is open source, you can find the method called to do the conversion and call automatically with a special command line argument you can add and pass to SharpDevelop. P.S.- That's the way developerfusion does the conversion (you can see in "Known Issues" a clear reference to #develop).



回答4:

I use the online tool http://www.developerfusion.com/tools/convert/csharp-to-vb/ This seems to handle whole classes without an error.



回答5:

You don't compile to VB. Languages used by the .NET Framework compile to CIL code, which you could decompile to VB.NET. The results would be ugly.

Instead, use a conversion tool. Your sample code gave me this:

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

        End Sub
    End Class
End Namespace


标签: c# vb.net roslyn