C#: Inconsistent accessibility: property type

▼魔方 西西 提交于 2019-11-26 23:12:03

问题


What's wrong with

public partial class MainWindow : Window
{
    public ObservableCollection<TabViewModel> Tabs { get; set; }
    public ICollectionView TabsViewSource { get; set; }
    public int CurrentIndex { get { return TabsViewSource.CurrentPosition; } }

I get

Inconsistent accessibility: property type 'System.Collections.ObjectModel.ObservableCollection' is less accessible than property 'TabsRendering.MainWindow.Tabs'

when i change the code to

public partial class MainWindow : Window
{
    ObservableCollection<TabViewModel> Tabs { get; set; }
    public ICollectionView TabsViewSource { get; set; }
    public int CurrentIndex { get { return TabsViewSource.CurrentPosition; } }

It works. Whats wrong with the public on the ObservableCollection


回答1:


MakeTabViewModela public type too.

Obviously, it doesn't make sense for a public property on a public containing-type to be of a type that is not public. How could the property present itself to external assemblies?

Your second sample works because, as a general rule, providing no accessibility modifiers means that the least applicable modifier is chosen as the default - in this case: private. Clearly, there are no consistency issues with declaring a private property of an internal (?) type.




回答2:


What's the accessibility on TabViewModel? I'm guessing it's not public.




回答3:


The message is very straight-forward. It is contradicting to what you want to do. It says you have something declared as public (Tabs, in this case) but the guy who would be using it also need to know about TabViewModel which is not public. Either make both public or some consistent access specifier.




回答4:


All of the information above is completely correct and works fine. I just want to add from personal experience that if you are using TFS and getting your project from TFS, different Visual Studio versions can also generate this error.

I entered a project with Visual Studio 2013 update 2 and synched with the TFS to get the solution. When I tried to run the project I got 80 errors. All of them were like "... less accessible than property...". Now it turns out I needed update 4. Once my Visual Studio was update I revered the changes and it worked perfectly.

This might be useful if none of the above works and you are using TFS.




回答5:


class Tbl_Selected:BaseEntity
{
    public int StudentId { get; set; }

    public int CourseId { get; set; }

    [ForeignKey(nameof(StudentId))]
    public Tbl_Student Tbl_Student { get; set; }

    [ForeignKey(nameof(VahedId))]
    public Tbl_Course Tbl_Course { get; set; }
}

this code to

public class Tbl_Selected:BaseEntity
    {
        public int StudentId { get; set; }

        public int CourseId { get; set; }

        [ForeignKey(nameof(StudentId))]
        public Tbl_Student Tbl_Student { get; set; }

        [ForeignKey(nameof(VahedId))]
        public Tbl_Course Tbl_Course { get; set; }
    }

You Most Add public To Your class



来源:https://stackoverflow.com/questions/3992928/c-inconsistent-accessibility-property-type

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!