c# - .NET:接口(interface)问题 VB.net Getter Only Interface

标签 c# .net vb.net

为什么接口(interface)会覆盖定义并违反类封装?我在下面包含了两个示例,一个在 C# 中,一个在 VB.net 中?

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 中实现一个允许私有(private) setter 接口(interface)。例如在 C# 中我可以声明:

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 中将 interface 属性声明为 readonly,则无法创建 setter。如果我创建一个 VB.net interface 作为普通的旧 property,那么接口(interface)声明将违反我的封装

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 中定义一个只有 getter 的接口(interface)

我认为第一个例子是最好的方法。但是,接口(interface)定义似乎凌驾于类定义之上。所以我尝试像在 C# 中一样创建一个getter only (Readonly) property,但它不适用于 VB.net。也许这只是语言的限制?

更新

根据 Hans Passant 的评论,我提交了一个功能请求:https://connect.microsoft.com/VisualStudio/feedback/details/635591/create-a-readonly-interface-that-allows-private-setters-c-to-vb-net-conversion

如果您也想要相同的兼容性功能,请为它投票!

最佳答案

下面是我在 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

关于c# - .NET:接口(interface)问题 VB.net Getter Only Interface,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4662352/

相关文章:

C#:如何使此方法成为非递归的

c# - 在纯 XAML 中,是否可以动态选择字符串格式?

c# - 为什么此参数不符合 CLS?

c# - ASP.NET MVC 中的准确 session 跟踪

.net - 如何在WPF中应用多种样式

c# - 加载一定数量的文件而不是扫描并加载目录中的所有文件

c# - 求职面试测试

c# - 如何在 xml 反序列化期间忽略无效的枚举值?

asp.net - 如何迭代自定义 vb.net 对象的每个属性?

c# - 为什么我的测试项目没有出现在测试资源管理器中