65.9K
CodeProject 正在变化。 阅读更多。
Home

VB6 C# VB 代码转换器

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.49/5 (44投票s)

2011 年 10 月 2 日

CPOL

12分钟阅读

viewsIcon

360636

downloadIcon

7130

代码转换。专注于将 VB6 升级到 VB.Net,以及转换 C# 和 VB.Net,并列出了许多其他有用的转换器,包括 php、java 等。

代码转换。专注于将 VB6 升级到 VB.Net,以及转换 C# <=> VB.Net,并列出了许多其他有用的转换器,包括 php、java 等。

目录:

下载次数

C# & VB.Net 转换器(使用 NRefactory v4)下载:

C# & VB.Net 转换器(不使用 NRefactory v4)下载:

VB6 升级器下载:

C# & VB 辅助库及相关代码下载:

PHP 到 .NET 转换器下载:

Java & .NET 转换器下载:

C++ 到 .NET 转换器下载:

ColdFusion 到 .NET 转换器下载:

转换前:

  • 在开始转换之前,请确保您的代码可以成功重新编译和运行。
  • 对于 C# 和 VB.Net,在许多情况下可以实现 100% 的代码转换,但并非总是如此。在许多情况下,需要进行大量的后期转换修复。
  • 没有转换器能保证 100% 的转换。
  • 对于其他语言对:请确保目标语言支持您的项目类型。
  • 请注意,VB6 转换是一项艰巨的任务,因为编程语言的理念不同。请在本文稍后部分查看 VB6 部分。

C# & VB.Net:

SharpDevelop:

市面上有许多 C# <> VB 代码转换器,几乎所有这些都使用 SharpDevelop IDE 的 Refactory 库进行转换,然后进行一些有用的后期转换修复。

NRefactory v4 的强大之处在于在转换之前解析代码;即使一个关键字可能具有多种含义,由于在转换之前理解了代码行,它也可以被正确转换。其他一些事情使得转换在大多数情况下成为可能。

关于 SharpDevelop

  • 它是一个免费的 .NET 开源开发环境。
  • 它可用于进行许多有用的代码转换。
  • SharpDevelop IDE 是 NRefactory v4 库的母体,它是最常用的转换库。
  • 使用 SharpDevelop IDE 进行的转换效果更好。
  • 它提供广泛的代码转换:C#、VB.NET、Boo、Python、Ruby

关于 NRefactory v4

  • ICSharpCode.NRefactory 作为 SharpDevelop IDE v4 的一部分免费提供。
  • 它是 C# 和 VB 的解析器库。
  • 它由一个抽象语法树 (AST) 组成,该树可以表示 C# 或 VB 中提供的所有构造(与 System.CodeDom 不同,后者仅表示 C# 和 VB 共有的构造)。
  • 通过使用 C# 解析器和 VB 输出访问器(或反之),您可以创建一个代码转换器。
  • 解析到 AST 后,您可以分析和/或转换 AST,并从(修改后的)AST 重新创建源代码,然后将我们从解析器中保存的注释重新插入到输出中。
  • 有关 NRefactory v4 的更多信息,请参阅:sharpdevelop.netNRefactory wiki
  • 您可以尝试 SharpDevelop 源代码中的 samples\NRefactoryDemo 来了解 AST 如何解析源代码。

关于 AST 和解析

以下示例展示了一个简单的解析过程。

Parsing

SharpDevelop IDE 和 C# VB 转换器

Application 转换
技术

转换
转换 注释
SharpDevelop IDE 开发者 IDE 免费软件 代码 & 文件 C#, VB.Net,
boo, Python, Ruby
 
VB.Net 到 C# 转换器 使用 NRefactory v4 代码 & 文件 VB.Net > C#  
Convert .NET 使用 NRefactory v4 仅代码 C# <> VB.Net  
C# 到 VB.NET 项目转换器 使用 NRefactory v4 项目 C# > VB.Net  
Instant VB 共享软件
内部转换器
代码、文件
& 文件夹
特定版本适用于
每次转换
在...之间转换
C#, VB.Net, C++,
Java
非常灵活

 

C# 中没有等效项的内容在 VB 中:

  • C# 固定大小缓冲区和 fixed 语句在 VB 中没有等效项
    private fixed char name[30]
  • C# fixed 块在 VB 中没有等效项
    输入 fixed 关键字。当它用于语句块时,它告诉 CLR 该对象不会被移动,因此它会固定该对象。因此,当 C# 中使用指针时,fixed 关键字通常用于防止运行时出现无效指针。
  • C# 泛型索引器和属性在 VB 中没有等效项
    public T this[string key] 
    { 
    	get { /* Return generic type T. */ } 
    }
    public T GetItem<T>(string key) 
    { 
    	/* Return generic type T. */ 
    }
    
  • C# 泛型运算符在 VB 中没有等效项
    public static T operator +<T>(T a, T b)	
    {
    // Do something with a and b that makes sense for operator + here
    }
  • C# #undef 在 VB 中没有等效项。
  • C# volatile/checked/unchecked/stackalloc 在 VB 中没有等效项
    使用 unchecked 语句处理常量表达式时,在编译时或运行时不会检查溢出,返回值也会有所不同。
  • C# unsafe 块在 VB 中没有等效项
    unsafe static void FastCopy ( byte[] src, byte[] dst, int count )
    {
    // unsafe context: can use pointers here
    }
  • C# extern alias 在 VB 中没有等效项
    有时需要引用具有相同完全限定类型名称的程序集的两个版本,例如,当您需要在同一个应用程序中使用两个或多个程序集版本时。通过使用外部程序集别名,每个程序集的命名空间可以包装在以别名命名的根命名空间内,从而允许在同一个文件中使用它们。
    要引用具有相同完全限定类型名称的两个程序集,必须在命令行上指定别名,如下所示:
    /r:GridV1=grid.dll
    /r:GridV2=grid20.dll 
    这将创建外部别名 GridV1 和 GridV2。要从程序内部使用这些别名,请使用 extern 关键字进行引用。例如:
    extern alias GridV1; 
    extern alias GridV2;
  • C# 重载 ++ 或 -- 运算符在 VB 中没有等效项:++ & -- 可以轻松转换,但重载不行。
  • C# ?? 运算符在 VB 中没有等效项

VB 中没有等效项的内容在 C# 中:

  • VB 参数化属性在 C# 中没有等效项
    Public Property MyProperty(ByVal A As String) As String
        Get
            Return 2 * A
        End Get
        Set(ByVal value As String)
            A = value / 2
        End Set
    End Property
  • VB 数字标签、On Error、Resume Next、Continue 和 Err 对象在 C# 中没有等效项
    Sub Test()
        On Error Resume Next
        Err.Raise(1000)
        On Error GoTo 0
        Err.Raise(1001)
        On Error GoTo -1
        Err.Raise(1002)
        On Error GoTo EH
        Err.Raise(1003)
        Exit Sub
    EH:
        MsgBox(Err.Description)
        Resume Next
    End Sub
  • VB IIf 在某种程度上不同于 C# 中的 if。
    A = IIf(E, B, A)
  • VB ReDim Preserve 在 C# 中没有内置等效项
    ReDim Preserve A(9)
  • VB Select Case:switch 不如 Select Case 灵活,许多 VB Select Case 块无法转换为 switch。
    Select Case A
        Case 1 To 10
            '
        Case Is < 2 * A
            '
        Case Is > 30
            '
    End Select
  • VB 过程局部静态变量在 C# 中没有内置等效项,但可以使用类级别的私有变量。
    Sub Test()
        Static InUse As Boolean = False
        If InUse Then Exit Sub
        'Run Once code
        InUse = True
        '
    End Sub
  • VB With 在 C# 中没有等效项。
    With Me
        .Width = 10
        .Top = 0
    End With
  • C# 不支持 VB Import Shared Class 成员
    Imports WindowsApplication1.Form1
  • VB Exit try
  • VB Exit 语句不匹配紧邻的块。
  • VB 成员名称可以与其包含的类型名称相同,但在 C# 中不行。
  • VB #Const 可以设置为任何值,但在 C# 中只接受常量。
    #Const C = 2 * A
  • VB MyClass 在 C# 中没有等效项(有时使用此项也可以)。
  • 在 C# 中,对象不能在其类级别的声明中引用自身:您不能在 C# 的类级别声明中使用 this。
  • 在 C# 中,对象不能在其类级别的声明中引用其基类:您不能在 C# 的类级别声明中使用 base。
  • C# 不允许使用数字标签:您必须将标签更改为标准的 C# 标识符。
  • VB &O 在 C# 中没有等效项
  • VB When 在 C# 中没有等效项
  • VB 包含一些 C# 中不存在且无法以任何方式重载的运算符(例如,整数除法 (VB \)、Like、幂运算 (VB ^))。
  • VB 方法名与类名相同,在 C# 中用作构造函数,不能用于其他任何目的。
  • VB 整数转换将“True”转换为 -1,但 System.Convert 方法将“True”转换为 1。

C# 和 VB 语言等效项和比较

  1. 语言等效项
  2. C# 和 VB 比较
  3. VB.NET & C# 完全比较

使用 SharpDevelop 作为代码转换器

  1. 运行 SharpDevelop IDE
  2. 从文件菜单选择打开 - 项目/解决方案,然后打开要转换的项目。
  3. 从视图菜单选择项目
  4. 在项目窗口中选择要转换的项目
  5. 从项目菜单中选择转换,然后选择目标语言。
  6. 这将转换整个项目。
  7. 可以通过右键单击项目窗口中的项目图标,然后选择转换来完成转换。

SharpDevelop 亮点

如何转换 C# 代码

代码使用 ICSharpCode.NRefactory.ParserFactory 类进行转换。

VB 中的转换器代码

Imports ICSharpCode.NRefactory
Imports ICSharpCode.NRefactory.PrettyPrinter
Imports System.IO

Public Class CS2VB

    Public Overrides Function ConvertCode(ByVal sourceCode As String) As String        
            
            Dim specials As IList(Of ISpecial)
            Dim result As Ast.CompilationUnit
            Dim Parse_errors = ""
            Using parser As IParser = ParserFactory.CreateParser( _
                 SupportedLanguage.CSharp, New StringReader(sourceCode))
                parser.Parse()
                'this allows retrieving comments, preprocessor directives, etc. 
                '(stuff that isn't part of the syntax)
                specials = parser.Lexer.SpecialTracker.RetrieveSpecials()
                'this retrieves the root node of the result AS
                result = parser.CompilationUnit
                If parser.Errors.Count > 0 Then
                    Parse_errors = parser.Errors.ErrorOutput
                End If
            End Using

            'Now you can analyze and/or transform the AST.
            'Should you want to re-create source code from the (modified) AST, 
            'use an output visitor: 
            're-insert the comments we saved from the parser into the output
            
            Dim outputVisitor As New PrettyPrinter.VBNetOutputVisitor
            Dim astViewUnit = result
            Using SpecialNodesInserter.Install(specials, outputVisitor)
                astViewUnit.AcceptVisitor(outputVisitor, Nothing)
            End Using
            Dim outputCode = outputVisitor.Text
            'By using the C# parser and a VB output visitor (or vice versa), 
            'you can build a code converter. Of course, 
            'the real Code Converter in SharpDevelop also transforms the AST to fix cases 
            'where C# and VB semantics differ.

            Return outputCode   

    End Function
End Class/pre>

C# 中的转换器代码

using ICSharpCode.NRefactory;
using ICSharpCode.NRefactory.PrettyPrinter;
using System.IO;

public class CS2VB
{

    public override string ConvertCode(string sourceCode)
    {
        //var names and format is as in http://wiki.sharpdevelop.net/NRefactory.ashx

        //ICSharpCode.NRefactory is a parser library for C# and VB.
        //It consists of a single Abstract Syntax Tree (AST) 
        //that can represent all constructs that are available in C# or VB 
        //(unlike System.CodeDom, which only represents constructs common to C# 
        //and VB).
        //Please try samples\NRefactoryDemo in the SharpDevelop source code 
        //to take a look at the AST
        //To parse source code, use: 
        IList specials;
        Ast.CompilationUnit result;
        object Parse_errors = "";
        using (IParser parser = ParserFactory.CreateParser(SupportedLanguage.CSharp, new 
                StringReader(sourceCode))) {
            parser.Parse();
            // this allows retrieving comments, preprocessor directives, etc. 
            //(stuff that isn't part of the syntax)
            specials = parser.Lexer.SpecialTracker.RetrieveSpecials();
            // this retrieves the root node of the result AST
            result = parser.CompilationUnit;
            if (parser.Errors.Count > 0) {
                MessageBox.Show(parser.Errors.ErrorOutput, "Parse errors");
            }
        }
        //Now you can analyze and/or transform the AST.
        //Should you want to re-create source code from the (modified) AST, 
        //use an output visitor: 
        //re-insert the comments we saved from the parser into the output
        PrettyPrinter.VBNetOutputVisitor outputVisitor = new 
                PrettyPrinter.VBNetOutputVisitor();
        object astViewUnit = result;
        using (SpecialNodesInserter.Install(specials, outputVisitor)) {
            astViewUnit.AcceptVisitor(outputVisitor, null);
        }
        object outputCode = outputVisitor.Text;
        //By using the C# parser and a VB output visitor (or vice versa), 
        //you can build a code converter. Of course, 
        //the real Code Converter in SharpDevelop also transforms the AST 
        //to fix cases 
        //where C# and VB semantics differ.

        return outputCode;

    }
}

演示转换

不支持部分代码转换,代码应在类和方法中。

演示 1

class Class1{void sub1(){
    //your code start
    int x =0;
    //your code end
}}

VB 中的结果

Class Class1
    Sub sub1()
        'your code start
        Dim x As Integer = 0
        'your code end
    End Sub
End Class

演示 2

namespace WindowsFormsApplication1{
    partial class Form1
    {
        private System.ComponentModel.IContainer components = null;
		private System.Windows.Forms.TextBox textbox1;
        private System.Windows.Forms.TextBox textbox2;

        
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.textbox1 = new System.Windows.Forms.TextBox();
            this.textbox2 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // textbox1
            // 
            this.textbox1.Location = new System.Drawing.Point(30, 30);
            this.textbox1.Name = "TEXTBOX1";
            this.textbox1.Size = new System.Drawing.Size(80, 20);
            this.textbox1.TabIndex = 0;
            // 
            // textbox2
            // 
            this.textbox2.Location = new System.Drawing.Point(30, 60);
            this.textbox2.Name = "textbox";
            this.textbox2.Size = new System.Drawing.Size(80, 20);
            this.textbox2.TabIndex = 1;
            this.textbox2.TextChanged += new System.EventHandler(this.textbox_TextChanged);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.textbox2);
            this.Controls.Add(this.textbox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }
        
        public Form1()
        {
            InitializeComponent();
        }

        private void textbox_TextChanged(object sender, EventArgs e)
        {
            string x = "";
            string X = "";
        }
    }
}

VB 中的结果

Namespace WindowsFormsApplication1
	Partial Class Form1
		Private components As System.ComponentModel.IContainer = Nothing
		Private textbox1 As System.Windows.Forms.TextBox
		Private textbox2 As System.Windows.Forms.TextBox


		Protected Overrides Sub Dispose(disposing As Boolean)
			If disposing AndAlso (components IsNot Nothing) Then
				components.Dispose()
			End If
			MyBase.Dispose(disposing)
		End Sub

		Private Sub InitializeComponent()
			Me.textbox1 = New System.Windows.Forms.TextBox()
			Me.textbox2 = New System.Windows.Forms.TextBox()
			Me.SuspendLayout()
			' 
			' textbox1
			' 
			Me.textbox1.Location = New System.Drawing.Point(30, 30)
			Me.textbox1.Name = "TEXTBOX1"
			Me.textbox1.Size = New System.Drawing.Size(80, 20)
			Me.textbox1.TabIndex = 0
			' 
			' textbox2
			' 
			Me.textbox2.Location = New System.Drawing.Point(30, 60)
			Me.textbox2.Name = "textbox"
			Me.textbox2.Size = New System.Drawing.Size(80, 20)
			Me.textbox2.TabIndex = 1
			AddHandler Me.textbox2.TextChanged, New System.EventHandler(AddressOf Me.textbox_TextChanged)
			' 
			' Form1
			' 
			Me.AutoScaleDimensions = New System.Drawing.SizeF(6F, 13F)
			Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
			Me.ClientSize = New System.Drawing.Size(284, 262)
			Me.Controls.Add(Me.textbox2)
			Me.Controls.Add(Me.textbox1)
			Me.Name = "Form1"
			Me.Text = "Form1"
			Me.ResumeLayout(False)
			Me.PerformLayout()

		End Sub

		Public Sub New()
			InitializeComponent()
		End Sub

		Private Sub textbox_TextChanged(sender As Object, e As EventArgs)
			Dim x__1 As String = ""
			Dim X__2 As String = ""
		End Sub
	End Class
End Namespace

转换案例

区分大小写的 C# 到 VB

转换器将自动重命名对象(如果需要)。

事件处理程序 C# 到 VB

C# 事件处理程序将转换为如下:

C#

this.textbox2.TextChanged += new System.EventHandler(this.textbox_TextChanged);

VB

AddHandler Me.textbox2.TextChanged, New System.EventHandler(AddressOf Me.textbox_TextChanged)

VB6 & VB.Net:

为什么 VB6 升级很重要

许多程序员喜爱 VB6 的魔术属性

  • 按需编译:您可以选择仅在调试时按需编译源代码。
  • 编辑并继续:在调试模式下,修复错误或编辑代码后,您不必强制重新启动应用程序。
  • 运行时不需要 .net framework。
  • COM 和 OLE 兼容。

VB 6.0 升级优势

  • 支持 64 位
  • 运行速度更快
  • 更多资源
  • 改进应用程序的维护性
  • 提高开发人员的生产力

VB 6.0 升级缺点

  • 按需编译将丢失。
  • 生成的应用程序需要 .net framework,而原始应用程序则不需要。
  • 如果您的应用程序是动态库,请注意 C# 或 VB.Net 不支持预声明对象。
  • 如果您的应用程序是 ActiveX 控件或 ActiveX 文档,那么没有可接受的转换器;需要手动重写代码。
  • 转换器可以节省您的转换时间,但后期转换工作是必不可少的。

什么是预声明对象?

我用 VB6 编写了一个库,用作 Office VBA 的插件。用户在使用我的库时会编写以下代码:

Sub Test()
    If Not NP.File.Exists("C:\Temp.txt") Then NP.File.Create "C:\Temp.txt"
    NP.File.Move "C:\Temp.txt", "C:\Temp2.txt"
    NP.File.Copy "C:\Temp2.txt", "C:\Temp.txt"
    NP.File.Delete "C:\Temp2.txt"
    NP.Shell "C:\Temp.txt"
End Sub

NP 对象是预先声明的,无需在 VBA 中声明。在 VBA Extend 的 VB.Net 版本中,用户应该添加额外的行。用户在使用对象之前应该首先声明一个名为 NP 的对象。

Public NP As New VBAExtend.NP
Sub Test()
    If Not NP.File.Exists("C:\Temp.txt") Then NP.File.Create "C:\Temp.txt"
    NP.File.Move "C:\Temp.txt", "C:\Temp2.txt"
    NP.File.Copy "C:\Temp2.txt", "C:\Temp.txt"
    NP.File.Delete "C:\Temp2.txt"
    NP.Shell "C:\Temp.txt"
End Sub

转换前

  • 最好安装 VB6,但并非必须。
  • 确保您的应用程序可以重新编译并正确运行。
  • 确保您引用的所有库在当前操作系统中都可用且运行良好。
  • 在开始转换之前,请确保您的代码可以成功重新编译和运行。
  • 没有转换器能保证 100% 的转换。
  • 确保 .net 语言支持您的项目类型。
  • 请注意,VB6 转换是一项艰巨的任务,因为编程语言的理念不同。VB6

VB6 升级助手 VBUC:

  • 如果您熟悉 VB6 升级,那么您会了解此软件的有用性。
  • 它节省了大量的升级时间。
  • 在大多数情况下需要大量的后期转换工作。
  • 它比 VS 2003、2005 和 2008 版本中包含的旧升级引擎要好得多。

VBUC 可以升级什么

  • 模块、类、窗体 & MDIForms
  • 在同一项目或同一组中使用的用户控件
  • 资源文件

VBUC 无法升级什么

  • 属性页
  • 设计器文件 (*.Dsr)
  • 在 Web 浏览器或 ActiveX 容器中使用的用户控件
  • ActiveX 文档

使用 VBUC 前

VBUC 是一个功能丰富的软件,有许多选项,我们应该在开始转换之前进行学习。

  • 转换为 VB.Net 比 C# 更简单。
  • 对于 ActiveX,选择“COM 可见”。
  • 窗体选项:尽可能使用辅助类。
  • 为了让您的应用程序更像 .net 设计,请使用“更多 .net”选项。
  • 如果以上选项失败,请使用“更多自动化”选项以减少转换中的错误。
  • 转换完成后,在 VS 中打开您的项目,禁用所有警告,并关闭 Option Explicit 和 Option Strict。
  • 如果仍然出现错误,请尝试手动修复。
  • 一旦您可以编译项目,请尝试关注警告。
  • 开启 Option Explicit 和 Option Strict,然后关注出现的任何错误。

VBUC 免费版限制

它适用于代码行数少于 30,000 行的项目。

VB 迁移:

  • 功能强大的新 VB6 转换应用程序。
  • 如果您熟悉 VB6 升级,那么您会了解此软件的有用性。
  • 它节省了大量的升级时间。
  • 在大多数情况下需要大量的后期转换工作。
  • 它比 VS 2003、2005 和 2008 版本中包含的旧升级引擎要好得多。
  • 它比 VBUC 更简单、更快。

属性

  • 支持大多数主要的 VB6 功能。
  • 试用软件的转换在公司服务器上进行。
  • 大型项目的快速转换。
  • .NET 应用程序将看起来和行为与原始 VB6 代码一样。
  • 简单的界面
  • 强大的支持库
  • 集成的代码编辑器
  • 允许分阶段迁移,部分转换。
  • 可扩展性
  • 用户控件将得到升级。
  • DLL 项目中的 Sub Main & 多线程组件
  • VBMP 的支持库中的控件公开与原始成员同名的属性和方法,这确保了代码即使在后期绑定场景下也能正常运行。
  • 自动实现的属性

VB 迁移无法升级什么

  • 属性页
  • 设计器文件 (*.Dsr)
  • 在 Web 浏览器或 ActiveX 容器中使用的用户控件
  • ActiveX 文档

您可以在 功能比较表 中看到。

其他 VB6 升级链接

© . All rights reserved.