简体   繁体   中英

Roslyn: Convert C# to VB

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?

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

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 .

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. PS- That's the way developerfusion does the conversion (you can see in "Known Issues" a clear reference to #develop).

我使用在线工具http://www.developerfusion.com/tools/convert/csharp-to-vb/这似乎处理整个类没有错误。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM