与受保护的内部成员不一致的可访问性 [英] Inconsistent accessibility with protected internal member
问题描述
尝试在公共类中创建受保护内部类的受保护内部成员会导致以下问题:
Attempting to make a protected internal member of a protected internal class within a public class results with the following issue:
不一致的可访问性:字段类型what.Class1.ProtectedInternalClass"比字段更难访问'what.Class1.SomeDataProvider.data'
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.
我哪里弄错了?
起源类:
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.
}
}
}
推荐答案
protected
适用于不同的类,这可以通过
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;
}
}
在不同的程序集中.
this.data
必须是可访问的,因为该类派生自 SomeDataProvider
.它的类型 ProtectedInternalClass
不可访问,因为该类不是从 Class1
派生的.
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
.
这篇关于与受保护的内部成员不一致的可访问性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!