开发者

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:

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

And I also have this class and implementation in 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#. I h开发者_Go百科ave the C# Interface converted over just fine, like so:

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. How can I call this method EncryptionVB instead of Encryption, but still implement the Encryption property?


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.

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. It would be accessible only as Encryption:

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

This is, in fact, behaving exactly the same as explicit interface implementation in 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.

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


This is not possible in C# - when implementing an interface, members must have the name defined in the interface.


VB.NET has lot of capabilities and few of them does not follow OOPS totally. One of such is this. C# does not allow to implement an interface using another name. I don't know why it is supported to VB.NET, may be because of backward compatibility.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜