一个C#到VB.Net转换实用程序,正确处理自动属性?

时间:2021-04-19 18:59:59

I hope this isn't considered a duplicate since it's more pointed than similar questions (I'm curious about a specific weakness in C# to VB.net conversion utilities).

我希望这不是一个重复,因为它比类似的问题更尖锐(我很好奇C#转换实用程序的特定弱点)。

I've been looking at using a tool like this .net code converter to convert a class library to VB since I'm the only one in my group comfortable with C#. The problem I've run into is that it doesn't generate proper VB for automatic properties. It creates empty get/set routines.

我一直在寻找使用类似.net代码转换器的工具将类库转换为VB,因为我是我团队中唯一一个熟悉C#的人。我遇到的问题是它没有为自动属性生成适当的VB。它创建空的get / set例程。

So this:

public string TransactionType { get; private set; }

Becomes this:

Public Property TransactionType() As String
    Get
    End Get
    Private Set(ByVal value As String)
    End Set
End Property

The tools linked here and here have similar issues - some create valid properties, but they don't respect the access level of the set routine.

此处和此处链接的工具具有类似的问题 - 一些创建有效的属性,但它们不尊重设置例程的访问级别。

Side question - If you were going to fix the converter on DeveloperFusion, would you have it return something like this?

附带问题 - 如果你打算在DeveloperFusion上修改转换器,你会让它返回这样的东西吗?

Private _TransactionType As String
Public Property TransactionType() As String
    Get
        Return _TransactionType
    End Get
    Private Set(ByVal value As String)
        _TransactionType = value
    End Set
End Property

5 个解决方案

#1


4  

We've now updated the code generator to support this scenario. If you spot any others that we're not doing very well, please do drop me a line.

我们现在已经更新了代码生成器以支持这种情况。如果你发现我们做得不好的任何其他人,请给我留言。

#2


2  

I'd recommend compiling the code and using something like Red-Gate's reflector http://www.red-gate.com/products/reflector/index.htm to handle the conversion. Now it's not "perfect," and I'm not sure if it handles automatic properties (though I'd imagine it would).

我建议编译代码并使用Red-Gate的反射器http://www.red-gate.com/products/reflector/index.htm来处理转换。现在它不是“完美”,我不确定它是否处理自动属性(虽然我想它会)。

What makes this possible is that when you compile .NET language down to IL they're exactly the same. The language is just another layer on top of that. So 2 properties that would look at the same in their native languages compile to the exact same IL code. So reversing this to other languages using something like Reflector is easy and quick.

使这成为可能的是,当您将.NET语言编译为IL时,它们完全相同。语言只是另一层。因此,在本地语言中看起来相同的2个属性将编译为完全相同的IL代码。因此,使用像Reflector之类的东西将其转换为其他语言非常简单快捷。

#3


2  

I stumbled on this while looking for a way to automate using reflector to translate code (since there are several plugins for it to generate code in other languages (even PowerShell)), but you made me wonder, so I tried it. With the compatibility set to .Net 3.5, it converts your example to this:

我在寻找一种自动使用反射器来翻译代码的方法时偶然发现了这一点(因为它有几个插件可以用其他语言生成代码(甚至是PowerShell)),但是你让我好奇,所以我试了一下。将兼容性设置为.Net 3.5,它将您的示例转换为:

Property TransactionType As String
  Public Get
  Private Set(ByVal value As String)
End Property

If you dig in, it does report that there are compiler generated methods which it doesn't export in VB.Net or C# with 3.5 compatability on ... HOWEVER, if you switch it to 2.0, the same code will generate this:

如果你深入研究,它确实报告有编译器生成的方法,它不会在VB.Net或C#中导出,具有3.5兼容性......但是,如果将其切换为2.0,相同的代码将生成:

Property TransactionType As String
    Public Get
        Return Me.<TransactionType>k__BackingField
    End Get
    Private Set(ByVal value As String)
        Me.<TransactionType>k__BackingField = value
    End Set
End Property

<CompilerGenerated> _
Private <TransactionType>k__BackingField As String

P.S.: if you try using a disassembler like Reflector to generate code, remember to keep the .pdb file around so you get proper names for the variables ;)

P.S。:如果您尝试使用像Reflector这样的反汇编程序来生成代码,请记住保留.pdb文件,以便获得变量的正确名称;)

#4


1  

As an answer to your side question: yes, that code is pretty much exactly what I'd get it to produce. You can't get it to do exactly what the C# code does, which is to make the name of the variable "unspeakable" (i.e. impossible to reference in code) but that's probably close enough.

作为你方面问题的答案:是的,那些代码几乎就是我要生成的代码。你无法让它完全按照C#代码所做的那样,即使变量的名称“难以言喻”(即代码中不可能引用),但这可能足够接近。

#5


1  

I would suggest checking out SharpDevelop (sometimes written as #develop). It's an Open Source .NET IDE that, among other things, can convert (with some issues) from C# to VB.NET and vice versa.

我建议查看SharpDevelop(有时写成#develop)。它是一个开源.NET IDE,除其他外,可以将(有些问题)从C#转换为VB.NET,反之亦然。

#1


4  

We've now updated the code generator to support this scenario. If you spot any others that we're not doing very well, please do drop me a line.

我们现在已经更新了代码生成器以支持这种情况。如果你发现我们做得不好的任何其他人,请给我留言。

#2


2  

I'd recommend compiling the code and using something like Red-Gate's reflector http://www.red-gate.com/products/reflector/index.htm to handle the conversion. Now it's not "perfect," and I'm not sure if it handles automatic properties (though I'd imagine it would).

我建议编译代码并使用Red-Gate的反射器http://www.red-gate.com/products/reflector/index.htm来处理转换。现在它不是“完美”,我不确定它是否处理自动属性(虽然我想它会)。

What makes this possible is that when you compile .NET language down to IL they're exactly the same. The language is just another layer on top of that. So 2 properties that would look at the same in their native languages compile to the exact same IL code. So reversing this to other languages using something like Reflector is easy and quick.

使这成为可能的是,当您将.NET语言编译为IL时,它们完全相同。语言只是另一层。因此,在本地语言中看起来相同的2个属性将编译为完全相同的IL代码。因此,使用像Reflector之类的东西将其转换为其他语言非常简单快捷。

#3


2  

I stumbled on this while looking for a way to automate using reflector to translate code (since there are several plugins for it to generate code in other languages (even PowerShell)), but you made me wonder, so I tried it. With the compatibility set to .Net 3.5, it converts your example to this:

我在寻找一种自动使用反射器来翻译代码的方法时偶然发现了这一点(因为它有几个插件可以用其他语言生成代码(甚至是PowerShell)),但是你让我好奇,所以我试了一下。将兼容性设置为.Net 3.5,它将您的示例转换为:

Property TransactionType As String
  Public Get
  Private Set(ByVal value As String)
End Property

If you dig in, it does report that there are compiler generated methods which it doesn't export in VB.Net or C# with 3.5 compatability on ... HOWEVER, if you switch it to 2.0, the same code will generate this:

如果你深入研究,它确实报告有编译器生成的方法,它不会在VB.Net或C#中导出,具有3.5兼容性......但是,如果将其切换为2.0,相同的代码将生成:

Property TransactionType As String
    Public Get
        Return Me.<TransactionType>k__BackingField
    End Get
    Private Set(ByVal value As String)
        Me.<TransactionType>k__BackingField = value
    End Set
End Property

<CompilerGenerated> _
Private <TransactionType>k__BackingField As String

P.S.: if you try using a disassembler like Reflector to generate code, remember to keep the .pdb file around so you get proper names for the variables ;)

P.S。:如果您尝试使用像Reflector这样的反汇编程序来生成代码,请记住保留.pdb文件,以便获得变量的正确名称;)

#4


1  

As an answer to your side question: yes, that code is pretty much exactly what I'd get it to produce. You can't get it to do exactly what the C# code does, which is to make the name of the variable "unspeakable" (i.e. impossible to reference in code) but that's probably close enough.

作为你方面问题的答案:是的,那些代码几乎就是我要生成的代码。你无法让它完全按照C#代码所做的那样,即使变量的名称“难以言喻”(即代码中不可能引用),但这可能足够接近。

#5


1  

I would suggest checking out SharpDevelop (sometimes written as #develop). It's an Open Source .NET IDE that, among other things, can convert (with some issues) from C# to VB.NET and vice versa.

我建议查看SharpDevelop(有时写成#develop)。它是一个开源.NET IDE,除其他外,可以将(有些问题)从C#转换为VB.NET,反之亦然。