.NET:界面问题VB.net Getter Only界面 [英] .NET: Interface Problem VB.net Getter Only Interface
问题描述
为什么接口会覆盖类的定义并违反类封装?我在下面提供了两个示例,一个在 C#中,一个在 VB.net 中?
Why does an interface override a class definition and violate class encapsulation? I have included two samples below, one in C# and one in VB.net?
Module Module1
Sub Main()
Dim testInterface As ITest = New TestMe
Console.WriteLine(testInterface.Testable) ''// Prints False
testInterface.Testable = True ''// Access to Private!!!
Console.WriteLine(testInterface.Testable) ''// Prints True
Dim testClass As TestMe = New TestMe
Console.WriteLine(testClass.Testable) ''// Prints False
''//testClass.Testable = True ''// Compile Error
Console.WriteLine(testClass.Testable) ''// Prints False
End Sub
End Module
Public Class TestMe : Implements ITest
Private m_testable As Boolean = False
Public Property Testable As Boolean Implements ITest.Testable
Get
Return m_testable
End Get
Private Set(ByVal value As Boolean)
m_testable = value
End Set
End Property
End Class
Interface ITest
Property Testable As Boolean
End Interface
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace InterfaceCSTest
{
class Program
{
static void Main(string[] args)
{
ITest testInterface = new TestMe();
Console.WriteLine(testInterface.Testable);
testInterface.Testable = true;
Console.WriteLine(testInterface.Testable);
TestMe testClass = new TestMe();
Console.WriteLine(testClass.Testable);
//testClass.Testable = true;
Console.WriteLine(testClass.Testable);
}
}
class TestMe : ITest
{
private bool m_testable = false;
public bool Testable
{
get
{
return m_testable;
}
private set
{
m_testable = value;
}
}
}
interface ITest
{
bool Testable { get; set; }
}
}
更具体地
如何在 VB.net 中实现界面,该界面将允许使用私人设置器.例如,在C#中,我可以声明:
More Specifically
How do I implement a interface in VB.net that will allow for a private setter. For example in C# I can declare:
class TestMe : ITest
{
private bool m_testable = false;
public bool Testable
{
get
{
return m_testable;
}
private set //No Compile Error here!
{
m_testable = value;
}
}
}
interface ITest
{
bool Testable { get; }
}
但是,如果我在 VB.net 中将界面属性声明为只读,则无法创建设置器.如果我创建一个VB.net 接口作为普通的属性,则接口声明将违反我的封装.
However, if I declare an interface property as readonly in VB.net I cannot create a setter. If I create a VB.net interface as just a plain old property then interface declarations will violate my encapsulation.
Public Class TestMe : Implements ITest
Private m_testable As Boolean = False
Public ReadOnly Property Testable As Boolean Implements ITest.Testable
Get
Return m_testable
End Get
Private Set(ByVal value As Boolean) ''//Compile Error
m_testable = value
End Set
End Property
End Class
Interface ITest
ReadOnly Property Testable As Boolean
End Interface
所以我的问题是,如何在VB.net中定义具有适当封装的仅获取接口?
So my question is, how do I define a getter only Interface in VB.net with proper encapsulation?
我认为第一个示例将是最好的方法.但是,似乎接口定义取代了类定义.因此,我尝试像在 C#中那样创建仅 getter(只读)属性,但是它不适用于 VB.net .也许这只是语言的局限性?
I figured the first example would have been the best method. However, it appears as if interface definitions overrule class definitions. So I tried to create a getter only (Readonly) property like in C# but it does not work for VB.net. Maybe this is just a limitation of the language?
根据Hans Passant的评论,我已经提交了发现的功能请求:
As per Hans Passant's comment I have submitted a feature request found : https://connect.microsoft.com/VisualStudio/feedback/details/635591/create-a-readonly-interface-that-allows-private-setters-c-to-vb-net-conversion
如果您也想要相同的兼容性功能,请投票!
Please vote for it if you would like the same compatibility feature as well!
推荐答案
这是我在VB.NET中的处理方式:
Here's how I'd do it in VB.NET:
Public Interface ITest
ReadOnly Property Testable As Boolean
End Interface
Public Class Test
Implements ITest
' Note: Here I am NOT implementing the interface. '
Private _testable As Boolean
Public Property Testable() As Boolean
Get
Return _testable
End Get
Private Set(ByVal value As Boolean)
_testable = value
End Set
End Property
' This is where I define a read-only property to satisfy the interface '
' (from the perspective of the VB compiler). '
' Notice this is a lot like explicit interface implementation in C#. '
Private ReadOnly Property TestableExplicit() As Boolean Implements ITest.Testable
Get
Return Testable
End Get
End Property
End Class
这篇关于.NET:界面问题VB.net Getter Only界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!