Roslyn: convert C # to VB

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

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

I tried to convert this class:

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

        }
    }
}

But it ended up:

Namespace TestApplication

    Class Class1

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

This happens not only when providing comments on XML documentation, but sometimes also when inheriting other classes, etc.

Here is the code that handles the conversion of the sample:

csharpToVisualBasicConverter.Convert returns an instance of SyntaxNode.

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());
}

convert, , SyntaxNode SyntaxNode.ToFullString() ..

- ?

+4
5
+2

SharpDevelop , . ( ) , , , , SharpDevelop. P.S.- , - ( " " #develop).

+1
+1

You are not compiling VB. The languages ​​used by the .NET Framework are compiled into CIL code , which you can decompile into VB.NET. The results would be ugly.

Use the conversion tool instead. Your sample code gave me the following:

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

        End Sub
    End Class
End Namespace
0
source

Source: https://habr.com/ru/post/1540708/


All Articles