【问题标题】:Inconsistent accessibility: property type in class is less accessible than property type in Interface可访问性不一致:类中的属性类型比接口中的属性类型更难访问
【发布时间】:2014-03-26 17:04:50
【问题描述】:

不确定这里出了什么问题,但我正在尝试在我创建的界面中使用枚举。

当我尝试实现接口时出现错误

可访问性不一致:属性类型“System.Collections.Generic.IList”的可访问性低于属性“BusinessEntities.ICloneMessage.AgentAddresses”

枚举

namespace BusinessEntities
{
    public class AddressTypeValues
    {
        [DataContract(Name = "AddressType")]
        public enum AddressType
        {
            [EnumMember(Value = "Home")]
            Home,
            [EnumMember(Value = "Mailing")]
            Mailing,
            [EnumMember(Value = "Location")]
            Location,
            [EnumMember(Value = "Other")]
            Other
        }
    }
}

界面

namespace BusinessEntities
{
    public interface IAgentAddress
    {
        AddressTypeValues.AddressType AddressType { get; set; }
        String? Street1 { get; set; }
        String? Street2 { get; set; }
        String? Street3 { get; set; }
        String? City { get; set; }
        String? State { get; set; }
        String? ZipCode { get; set; }
    }
}

使用 AddresType 的类

namespace BusinessEntities
{
    [DataContract]
    public class CloneMessage : ICloneMessage
    {
        [DataMember]
        public AgentTransmission AgentInformation { get; set; }
        [DataMember]
        public IList<AgentAddress> AgentAddresses { get; set; }
        [DataMember]
        public IList<RelationshipCode> RelationshipCodes { get; set; }
        [DataMember]
        public string? ErrorMessages { get; set; }
        public CloneMessage(){}
    }
}
namespace BusinessEntities
{
    [DataContract]
    public class AgentAddress : IAgentAddress
    {
        [DataMember]
        public AddressTypeValues.AddressType AddressType { get; set; }
        [DataMember]
        public string Street1 { get; set; }
        [DataMember]
        public string Street2 { get; set; }
        [DataMember]
        public string Street3 { get; set; }
        [DataMember]
        public string City { get; set; }
        [DataMember]
        public string State { get; set; }
        [DataMember]
        public string ZipCode { get; set; }
    }
}

【问题讨论】:

  • 你的班级AgentAddress可能是internal,把它设为公共班级
  • 如果我这样做,我被告知我不能在string 接口中拥有可为空的string 属性。
  • String 已经是一个可为空的类。你不能有Nullable&lt;String&gt;。去掉问号。
  • @Neal,那是完全不同的错误,string 已经是可空的,它是一个引用类型,删除 ?String?Nullable&lt;T&gt; 用于值类型,不是引用类型

标签: c# .net interface enums


【解决方案1】:

您的代码中有几个错误。

首先。您的班级 AgentAddress 未标记为 public。这就是您收到错误的原因。可能您的类没有使用任何访问说明符定义,它被视为internal。让你的班级public 将消除错误。

您的代码中的第二个问题是您使用的是String?。可能正在尝试将字符串设为Nullable&lt;T&gt;String已经是一个引用类型,可以为null。你需要用String?去掉?。所以你的界面看起来像:

public interface IAgentAddress
{
    AddressTypeValues.AddressType AddressType { get; set; }
    String Street1 { get; set; }
    String Street2 { get; set; }
    String Street3 { get; set; }
    String City { get; set; }
    String State { get; set; }
    String ZipCode { get; set; }
}

Nullable&lt;T&gt; 或带有? 的类型用于值类型,因为值类型不能像int?Nullable&lt;int&gt; 那样持有null

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-27
    • 2011-04-28
    • 1970-01-01
    相关资源
    最近更新 更多