C# 不一致的可访问性:属性类型的可访问性较差

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13660355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:21:17  来源:igfitidea点击:

Inconsistent accessibility: property type is less accessible

c#properties

提问by David Bukera

Please can someone help with the following error:

请有人帮助解决以下错误:

Inconsistent accessibility: property type 'Test.Delivery' is less accessible than property 'Test.Form1.thelivery'

不一致的可访问性:属性类型“Test.Delivery”的可访问性低于属性“Test.Form1.thelivery”

private Delivery thedelivery;

public Delivery thedelivery
{
    get { return thedelivery; }
    set { thedelivery = value; }
}

I'm not able to run the program due to the error message of inconsistency.

由于不一致的错误消息,我无法运行程序。

Here is my delivery class:

这是我的交付课程:

namespace Test
{
    class Delivery
    {
        private string name;
        private string address;
        private DateTime arrivalTime;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public string Address
        {
            get { return address; }
            set { address = value; }
        }

        public DateTime ArrivlaTime
        {
            get { return arrivalTime; }
            set { arrivalTime = value; }
        }

        public string ToString()
        {
            { return name + address + arrivalTime.ToString(); }
        }
    }
}

采纳答案by Ravindra Bagale

Declare your class with a publicaccess modifier instead by adding the publickeyword in front of the class name

使用public访问修饰符声明您的类,而不是public在类名前添加关键字

namespace Test
{
    public class Delivery
    {
        private string name;
        private string address;
        private DateTime arrivalTime;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public string Address
        {
            get { return address; }
            set { address = value; }
        }

        public DateTime ArrivlaTime
        {
            get { return arrivalTime; }
            set { arrivalTime = value; }
        }

        public string ToString()
        {
            { return name + address + arrivalTime.ToString(); }
        }
    }
}

回答by J. Steen

Your class Deliveryhas no access modifier, which means it defaults to internal. If you then try to expose a property of that type as public, it won't work. Your type (class) needs to have the same, or higher access as your property.

您的类Delivery没有访问修饰符,这意味着它默认为internal. 如果您随后尝试将该类型的属性公开为public,它将不起作用。您的类型(类)需要与您的属性具有相同或更高的访问权限。

More about access modifiers: http://msdn.microsoft.com/en-us/library/ms173121.aspx

有关访问修饰符的更多信息:http: //msdn.microsoft.com/en-us/library/ms173121.aspx

回答by Lee

Your Deliveryclass is internal (the default visibility for classes), however the property (and presumably the containing class) are public, so the property is more accessible than the Deliveryclass. You need to either make Deliverypublic, or restrict the visibility of the theliveryproperty.

你的Delivery类是内部的(类的默认可见性),但是属性(可能包含类)是公共的,所以属性比Delivery类更容易访问。您需要Delivery公开或限制该thelivery属性的可见性。