简体   繁体   English

Roslyn:将C#转换为VB

[英]Roslyn: Convert C# to VB

I have the case wherein I need to convert a C#- to a VB.NET project. 我有一个案例,我需要将C#转换为VB.NET项目。 (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. 一个“粘贴为C#/ VB”样本视觉工作室扩展 ,似乎能够做到这一点。

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. 它不仅发生在提供XML文档注释时,而且有时在继承其他类时也会发生。

Here's the code that handles the convertion of the sample: 这是处理样本转换的代码:

csharpToVisualBasicConverter.Convert returns a SyntaxNode instance. csharpToVisualBasicConverter.Convert返回一个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());
}

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. 由于调用convert方法时没有异常,我假设该方法返回一个有效的SyntaxNode,并且SyntaxNode.ToFullString()方法或编码问题弄乱了换行符等。

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. 我在7年前在VB.NET中开发了一个应用程序,我不得不将一个组件集成到其中,其SDK仅用C#编写。 The application that I had to integrate was reasonably large amd complex. 我必须整合的应用程序是相当大的amd复杂。 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. 我使用这个产品将C#转换为VB.NET,虽然成品确实需要一些调整和一些彻底的测试,但我不记得这个过程特别令人痛苦。 The outcome was excellent. 结果非常好。 The application worked well and it is still going strong today. 该应用程序运行良好,今天仍然很强劲。

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

FYI there is now a Roslyn Code Converter add-in for Visual Studio 2015.2+: 仅供参考,Visual Studio 2015.2+现在有一个Roslyn Code Converter加载项:

https://marketplace.visualstudio.com/items?itemName=SharpDevelopTeam.CodeConverter 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. SharpDevelop有一个菜单为整个项目进行转换。 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. 您可以自动化它与GUI交互(模拟鼠标点击),或者,因为它是开源的,您可以找到调用的方法进行转换,并使用您可以添加并传递给SharpDevelop的特殊命令行参数自动调用。 PS- That's the way developerfusion does the conversion (you can see in "Known Issues" a clear reference to #develop). PS-这就是developerfusion进行转换的方式(你可以在“已知问题”中看到#develop的明确引用)。

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

You don't compile to VB. 你不编译到VB。 Languages used by the .NET Framework compile to CIL code , which you could decompile to VB.NET. .NET Framework使用的语言编译为CIL代码 ,您可以将其反编译为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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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