Inconsistent accessibility with protected internal member

一世执手 提交于 2021-02-20 10:12:24

问题


Attempting to make a protected internal member of a protected internal class within a public class results with the following issue:

Inconsistent accessibility: field type 'what.Class1.ProtectedInternalClass' is less accessible than field 'what.Class1.SomeDataProvider.data'

The accessibility should be equivalent, as far as I know.

Where am I mistaken?

Origination class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace what
{
    public class Class1
    {
        // This class cannot be modified, is only 
        // here to produce a complete example.
        public class PublicClass
        {
            public PublicClass() { }
        }

        protected internal class ProtectedInternalClass : PublicClass
        {
            public ProtectedInternalClass() { }

            public void SomeExtraFunction() { }
        }

        public class SomeDataProvider
        {
            public int AnInterestingValue;
            public int AnotherInterestingValue;

            protected internal ProtectedInternalClass data; //<--- Occurs here.
            public PublicClass Data { get { return data; } }
        }


        public static SomeDataProvider RetrieveProvider()
        {
            SomeDataProvider provider = new SomeDataProvider();

            provider.data = new ProtectedInternalClass();
            provider.data.SomeExtraFunction();

            return provider;
        }

    }
}

Verifying protected and internal properties, same assembly:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace what
{
    public class Class2 : Class1
    {
        public Class2()
        {
            var pi = new ProtectedInternalClass();

            var provider = new SomeDataProvider();
            provider.data = pi;
        }
        // no errors here
    }

    public class Class3
    {
        public Class3()
        {
            var pi = new Class1.ProtectedInternalClass();

            var provider = new Class1.SomeDataProvider();
            provider.data = pi;
        }
        // no errors here
    }
}

Verifying protected and internal properties, different assembly:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace some_other_assembly
{
    public class Class4 : what.Class1
    {
        public Class4()
        {
            var pi = new ProtectedInternalClass();

            var provider = new SomeDataProvider();
            provider.data = pi;
        }
        // no errors here
    }

    public class Class5
    {
        public Class5()
        {
            var pi = new what.Class1.ProtectedInternalClass(); // <--- Inaccessible due to protection level, as it should be.

            var provider = new what.Class1.SomeDataProvider();
            provider.data = pi; // <--- Intellisense implies inaccessible, but not indicated via error.
        }
    }
}

回答1:


The protected applies to different classes, and this can be seen with

class Derived : what.Class1.SomeDataProvider // note: Derived is not a nested class
{
    public void f()
    {
        var data = this.data;
    }
}

in a different assembly.

this.data has to be accessible, since the class derives from SomeDataProvider. Its type, ProtectedInternalClass, is not accessible, since the class does not derive from Class1.



来源:https://stackoverflow.com/questions/18767334/inconsistent-accessibility-with-protected-internal-member

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