受保护的内部成员的可访问性不一致

时间:2013-09-12 14:41:22

标签: c# internal protected

尝试在公共类中创建受保护的内部类的受保护内部成员会导致以下问题:

  

可访问性不一致:字段类型   'what.Class1.ProtectedInternalClass'比字段更难访问   'what.Class1.SomeDataProvider.data'

据我所知,可访问性应该是等效的。

我错在哪里?

起源课程:

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;
        }

    }
}

验证受保护和内部属性,同一程序集:

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
    }
}

验证受保护和内部属性,不同的程序集:

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 个答案:

答案 0 :(得分:3)

protected适用于不同的类,可以通过

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

在另一个集会中。

this.data必须是可访问的,因为该类派生自SomeDataProvider。其类型ProtectedInternalClass无法访问,因为该类不是从Class1派生的。