c# - 无法实现接口(interface)成员,因为它不是公共(public)的

标签 c#

我使用 Sharp Develop IDE 编写了一些转换为 C# 的 VB 代码。 我定义了一个接口(interface) IElement,它由返回自身 XML 表示的对象实现。任何实现此接口(interface)的对象都应该能够返回其 TagName 及其 XML 字符串表示形式。要获取它的 XML 字符串,它可能必须遍历其子/嵌套集合以获取其所有子对象的 XML 表示。

从 Element 继承的类可以使用其基类的 GetXml 和 GetNestedXml 或选择覆盖它,但 GetNestedXml 函数不需要是公共(public)的,因为它只会从派生类的公共(public) GetXml 函数中调用。因此,在原来的VB版本中,GetNestedXML的作用域被设置为protected。但是,Sharp Develop 和我在尝试将此代码转换为 C# 时遇到问题。请查看下面的错误。

附带说明一下,我确实意识到可能有更好的方法来实现这一点,并且我会对容易引起轰动的附带建议感兴趣。 :-) 谢谢。

Public Interface IElement

    ReadOnly Property TagName() As String
    ReadOnly Property GetXml(Optional ByVal targetXml As Integer = TargetXmlEnum.All) As String
    Function GetNestedXml() As String

End Interface


Public Class Element

    Implements IElement

    Public ReadOnly Property TagName() As String Implements IElement.TagName
        Get
            '....
        End Get
    End Property

     Public Overridable ReadOnly Property GetXml(Optional ByVal targetXml As Integer = TargetXmlEnum.All) _
            As String Implements IElement.GetXml
        Get
            '....
        End Get
    End Property

    Protected Overridable Function GetNestedXml() As String Implements IElement.GetNestedXml
        '....
    End Function

End Class

转换后的 C#:

public interface IElement
{

    string TagName { get; }
    string GetXml { get; }
    string GetNestedXml();

}

public class Element : IElement
{
    public string TagName {
        get { //... }
    }

    public virtual string GetXml 
    {
        get 
        {
            //...
        }
    }

    protected virtual string GetNestedXml()
    {
        //...
    }
}

错误:

Error   1   'Smit.SpreadsheetML.Element' does not implement interface member 'Smit.SpreadsheetML.IElement.GetNestedXml()'. 'Smit.SpreadsheetML.Element.GetNestedXml()' cannot implement an interface member because it is not public.   D:\Users\Chad\Desktop\SMIT\SMIT.SpreadsheetML.ConvertedToC#\Element.cs  41  24  Smit.SpreadsheetML.Converted

最佳答案

由于接口(interface)实现需要公开或显式:

改变这个方法

protected virtual string GetNestedXml()
{
     //...
}

protected virtual string IElement.GetNestedXml()
{
    //...
}

编辑

像这样创建一个接口(interface):

public interface IElement
{
    string TagName { get; }
    string GetXml { get; }
}

像这样创建一个抽象基类

abstract class ElementBase:IElement

{
    public abstract string TagName { get; }
    public abstract string GetXml { get; }
    protected abstract string GetNestedXml();
}

实现你的元素类

public class Element : ElementBase
{
    public override string TagName {
        get { //... }
    }

    public override string GetXml 
    {
        get 
        {
            //...
        }
    }

    protected override string GetNestedXml()
    {
        //...
    }
}

关于c# - 无法实现接口(interface)成员,因为它不是公共(public)的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21898841/

相关文章:

c# - 你如何从 Jenkins 运行 NUnit 测试?

c# - 如何在 visual studio 中为数据表中的每一行设计一个页面

c# 使用默认应用程序和参数打开文件

C# HttpClient.SendAsync 始终返回 404 但 URL 在浏览器中有效

c# - 大数据损坏的 excel 文件

c# - Singleton 设计模式是否内置于任何框架中?

c# - 如何监听 SQL Server 数据库更改

c# - 更改 EditorTemplates 文件夹位置

c# - 与使用 GraphicsPath 的 Rectangle.Inflate() 等效的是什么?

c# - 根据行号删除多行