简体   繁体   English

将接口实现从VB.NET转换为C#

[英]Converting interface implementation from VB.NET to C#

This may seem like an obvious answer, but I can't seem to find an answer. 这似乎是一个明显的答案,但我似乎无法找到答案。 I have this code in VB.NET: 我在VB.NET中有这个代码:

Public Interface ITestInterface
    WriteOnly Property Encryption() As Boolean
End Interface

And I also have this class and implementation in VB.NET: 我在VB.NET中也有这个类和实现:

Partial Public Class TestClass
    Implements ITestInterface

    Public WriteOnly Property EncryptionVB() As Boolean Implements ITestInterface.Encryption
        Set(ByVal value As Booleam)
             m_Encryption = value
        End Set
    End Property
End Class

I am trying to convert this over to C#. 我试图将其转换为C#。 I have the C# Interface converted over just fine, like so: 我把C#接口转换得很好,就像这样:

public interface ITestInterface
{
    bool Encryption { set; }
}

The problem is, how to convert the implementation over. 问题是,如何将实现转换为。 I have this: 我有这个:

public partial class TestClass
{
    public bool Encryption 
    {
         set { m_Encryption = value; }
    }
}

The problem with this is that in C#, it would seem you have to name the function the same as the interface function you are implementing. 问题在于,在C#中,您似乎必须将该函数命名为与您正在实现的接口函数相同的名称。 How can I call this method EncryptionVB instead of Encryption, but still implement the Encryption property? 如何调用此方法EncryptionVB而不是Encryption,但仍然实现加密属性?

The closest way I can think of is to use explicit implementation: 我能想到的最接近的方法是使用显式实现:

public partial class TestClass : ITestInterface
{
    public bool EncryptionVB
    {
         ((ITestInterface)this).Encryption = value;
    }

    bool ITestInterface.Encryption { set; }
}

Now, on the surface this might seem like "not the same thing." 现在,从表面上看,这似乎“不一样”。 But it really is. 但确实如此。 Consider the fact that in VB.NET, when you name a member that implements an interface member something different from what the interface defines, this "new name" only appears when you know the type at compile time. 考虑一下这样的事实:在VB.NET中,当您为实现接口成员的成员命名与接口定义的成员不同时,只有在编译时知道类型时才会出现此“新名称”。

So: 所以:

Dim x As New TestClass
x.EncryptionVB = True

But if x in the above code were typed as ITestInterface , that EncryptionVB property would not be visible. 但是,如果上面代码中的x被键入为ITestInterface ,则该EncryptionVB属性将不可见。 It would be accessible only as Encryption : 它只能作为Encryption访问:

Dim y As ITestInterface = New TestClass
y.Encryption = True

This is, in fact, behaving exactly the same as explicit interface implementation in C#. 实际上, 这与 C#中的显式接口实现完全相同 Take a look at the equivalent code: 看一下等效的代码:

TestClass x = new TestClass();
x.EncryptionVB = true;

ITestInterface y = new TestClass();
y.Encryption = true;

C# doesn't support interface member aliasing as does VB.NET. C#不像VB.NET那样支持接口成员别名。

The best match would be something like this: 最好的匹配是这样的:

public partial class TestClass : ITestInterface{
  bool ITestInterface.Encryption {
    set { m_Encryption = value; }
  }

  public bool EncryptionVB {
    set { ((ITestInterface)this).Encryption = value; }
  }
}

Perform the change using protection level 使用保护级别执行更改

public bool EncryptionVB {
    set { m_Encryption = value; }
}
bool ITestInterface.Encryption {
    set { EncryptionVB = value; }
}

这在C#中是不可能的 - 在实现接口时,成员必须在接口中定义名称。

VB.NET has lot of capabilities and few of them does not follow OOPS totally. VB.NET有很多功能,很少有人不完全遵循OOPS。 One of such is this. 其中之一就是这个。 C# does not allow to implement an interface using another name. C#不允许使用其他名称实现接口。 I don't know why it is supported to VB.NET, may be because of backward compatibility. 我不知道为什么它支持VB.NET,可能是因为向后兼容。

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

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