Roslyn:将 C# 转换为 VB [英] Roslyn: Convert C# to VB

查看:28
本文介绍了Roslyn:将 C# 转换为 VB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到过需要将 C#- 转换为 VB.NET 项目的情况.(我想自动化这个,所以我不能使用在线工具或类似的东西)

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)

一个粘贴为 C#/VB"示例视觉工作室扩展似乎能够做到这一点.

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

我尝试转换这个类:

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

        }
    }
}

但结果是这样的:

Namespace TestApplication

    Class Class1

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

不仅在提供 XML 文档注释时会发生这种情况,有时在继承其他类等时也会发生这种情况.

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 返回一个 SyntaxNode 实例.

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

由于调用 convert 方法时没有例外,我假设该方法返回一个有效的 SyntaxNode 和 SyntaxNode.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?

推荐答案

我 7 年前用 VB.NET 开发了一个应用程序,我不得不将一个组件集成到其中,该组件的 SDK 仅用 C# 编写.我必须集成的应用程序相当大且复杂.我使用这个产品将 C# 转换为 VB.NET,虽然最终产品确实需要一些调整和一些彻底的测试,但我不记得这个过程特别令人痛苦.结果非常好.该应用程序运行良好,今天仍然很强大.

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

这篇关于Roslyn:将 C# 转换为 VB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆