开发者

C#: Inconsistent accessibility: property type

开发者 https://www.devze.com 2023-01-20 17:07 出处:网络
What\'s wrong with public partial class MainWindow : Window { public ObservableCollection<TabViewModel> Tabs { get; set; }

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 IColle开发者_如何转开发ctionView TabsViewSource { get; set; }
    public int CurrentIndex { get { return TabsViewSource.CurrentPosition; } }

It works. Whats wrong with the public on the ObservableCollection


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.


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号