首页 > 代码库 > [c#]可访问性不一致

[c#]可访问性不一致

【问题】


【分析】

返回类型和方法的形参表中引用的各个类型必须至少具有和方法本身相同的可访问性。

其中一个是返回参数的访问级别小于函数的访问级别,

也就是说当定义一个返回参数的方法的时候,如果返回参数的访问级别低于方法的访问级别就会出现这样的错误,

这个是可以理解的,如果返回的参数不能被访问,那么定义的返回的方法也是错误的.


【实例】

SmartPark.BLL.ParkTicketTypeManager:

public Park_TicektType GetByParkID(long parkID)
        {
            return null;
        }

SmartPark.Models.Park_TicketType:

namespace SmartPark.Models
{
    [Serializable()]
    <span style="color:#ff0000;">class Park_TicektType</span>
    {
        private int ticketType_Park_ID;

        public int TicketType_Park_ID
        {
            get { return ticketType_Park_ID; }
            set { ticketType_Park_ID = value; }
        }
        private int ticketTypeID;

        public int TicketTypeID
        {
            get { return ticketTypeID; }
            set { ticketTypeID = value; }
        }
        private string ticketTypeName;

        public string TicketTypeName
        {
            get { return ticketTypeName; }
            set { ticketTypeName = value; }
        }
        private int parkID;

        public int ParkID
        {
            get { return parkID; }
            set { parkID = value; }
        }
        private string parkName;

        public string ParkName
        {
            get { return parkName; }
            set { parkName = value; }
        }

        private bool deleteFlag;

        public bool DeleteFlag
        {
            get { return deleteFlag; }
            set { deleteFlag = value; }
        }
        private string demo;

        public string Demo
        {
            get { return demo; }
            set { demo = value; }
        }
    }
}

函数的访问级别为public,返回参数的访问级别为private,当定义一个返回参数的方法的时候,如果返回参数的访问级别低于方法的访问级别就会出现这样的错误



新建类的时候,class前面默认是没有public的,加上就对了

改成:

namespace SmartPark.Models
{
    [Serializable()]
    public class Park_TicektType
    {
        private int ticketType_Park_ID;

        public int TicketType_Park_ID
        {
            get { return ticketType_Park_ID; }
            set { ticketType_Park_ID = value; }
        }
        private int ticketTypeID;

        public int TicketTypeID
        {
            get { return ticketTypeID; }
            set { ticketTypeID = value; }
        }
        private string ticketTypeName;

        public string TicketTypeName
        {
            get { return ticketTypeName; }
            set { ticketTypeName = value; }
        }
        private int parkID;

        public int ParkID
        {
            get { return parkID; }
            set { parkID = value; }
        }
        private string parkName;

        public string ParkName
        {
            get { return parkName; }
            set { parkName = value; }
        }
        
        private bool deleteFlag;

        public bool DeleteFlag
        {
            get { return deleteFlag; }
            set { deleteFlag = value; }
        }
        private string demo;

        public string Demo
        {
            get { return demo; }
            set { demo = value; }
        }
    }
}







[c#]可访问性不一致