问题描述
我设置了一些演示code对无障碍初学者会话,我发现,我能够从派生类中访问的内部保护的财产。我在想什么?
I'm setting up some demo code for a beginner session on accessibility and I found that I am able to access an internal protected property from a derived class. What am I missing?
大会1
namespace Accessibility
{
class Program
{
static void Main(string[] args)
{
ExampleClass c = new ExampleClass();
c.Go();
//c.Prop1 = 10;
}
}
class ExampleClass : DerivedClass
{
public void Go()
{
this.Prop1 = 10;
this.Prop2 = 10;
//this.Prop3 = 10; //Doesn't work
//this.Prop4 = 10; //Doesn't work
this.Prop5 = 10; //why does this work?!
this.DoSomething();
}
}
}
大会2
namespace Accessibility.Models
{
public class BaseClass
{
public int Prop1 { get; set; }
protected int Prop2 { get; set; }
private int Prop3 { get; set; }
internal int Prop4 { get; set; }
internal protected int Prop5 { get; set; }
//internal public int Prop6 { get; set; } //Invalid
//internal private int Prop7 { get; set; } //Invalid
public BaseClass()
{
this.Prop3 = 27;
}
}
public class DerivedClass : BaseClass
{
public void DoSomething()
{
this.Prop1 = 10;
this.Prop2 = 10;
//this.Prop3 = 10; //Doesn't work
this.Prop4 = 10;
this.Prop5 = 10;
PropertyInfo Prop3pi = typeof(DerivedClass).GetProperty("Prop3", BindingFlags.Instance | BindingFlags.NonPublic);
int value = (int)Prop3pi.GetValue(this, null);
}
}
}
请注意,在ExampleClass.Go我可以设置一个值Prop5。为什么?它标记为内部的保护,但我不能设置上Prop4值(标记为内部的)
Notice in ExampleClass.Go I can set a value to Prop5. Why? It's marked as internal protected but I can't set a value on Prop4 (marked as internal)
推荐答案
内部保护
的意思是内部的组件或继承的类。所以,是的,如果你有一个公共类与保护的内部成员,另一个类继承该类型在不同的组件仍然可以访问,因为受保护的修饰符是:
internal protected
means "internal to the assembly OR an inherited class". So yes, if you have a public class with an protected internal member, another class that inherits that type in a different assembly can still access it because of the protected modifier:
受保护的内部
该类型或成员可以通过任何code在声明它的组件,可以访问或从内另一个组件派生类。从另一个程序访问必须发生在一个类声明派生自其中受保护的内部元素声明的类,它必须通过派生类类型的实例发生。
The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.
参考:http://msdn.microsoft.com/en-us/library/ms173121.aspx
这是C#语言的限制。 CLR的支持内部和保护的概念。有这方面的证据与<$c$c>MethodAttributes.FamANDAssem$c$c>枚举如果你发射自己的白细胞介素。如果你的真正的想要这个功能,你可以做的东西,如Mono.Cecil能做到一些IL后期处理。为什么C#语言不公开,这只是一个猜测:没有必要为它
This is a limitation of the C# language. The CLR supports the "Internal AND Protected" notion. There is evidence of this with the MethodAttributes.FamANDAssem
enumeration if you were emitting your own IL. If you really wanted this feature, you could do some IL post processing with something like Mono.Cecil. Why the C# language does not expose this is only a guess: little need for it.
这篇关于内部保护属性仍从不同的组件访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!