internal protected property still accessible from a different assembly

前端 未结 8 1140
清酒与你
清酒与你 2021-02-07 09:40

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 missin

8条回答
  •  佛祖请我去吃肉
    2021-02-07 10:32

    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:

    protected internal

    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.

    Reference: http://msdn.microsoft.com/en-us/library/ms173121.aspx

    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.

提交回复
热议问题