internal protected property still accessible from a different assembly

前端 未结 8 963
清酒与你
清酒与你 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:20

    Protected means that it is shared only with descendant classes, only private means that it can't be accessed by anyone else.

    Edit: Hans comment makes your question a little more clear. When you combine modifiers like that, they combine inclusively not exclusively. It is accesse in all the ways internal or protected can be.

    0 讨论(0)
  • 2021-02-07 10:22

    By combining the protected and internal keywords, a class member can be marked protected internal — only derived types or types within the same assembly can access that member.

    This MSDN article answers all your questions.

    0 讨论(0)
  • 2021-02-07 10:28

    The protected keyword is a member access modifier. A protected member is accessible from within the class in which it is declared, and from within any class derived from the class that declared this member.

    http://msdn.microsoft.com/en-us/library/bcd5672a(v=vs.71).aspx

    By combining the protected and internal keywords, a class member can be marked protected internal — only derived types or types within the same assembly can access that member.

    http://msdn.microsoft.com/en-us/library/ms173121(v=vs.80).aspx

    0 讨论(0)
  • 2021-02-07 10:29

    Late answer but I got caught with the same issue. Eventualy I came up with a partial solution.

        internal Int MyInt
        {
            get;
            protected set;
        }
    

    It is still visible within the assembly but at least only inherithing classes can actualy change it. It is enough for what I wanted.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-07 10:34

    Because it's how internal protected is intended to work. Access is given for either children in inheritance tree (protected part) or for the same assembly (internal part) - see Access Modifiers on MSDN.

    And your ExampleClass is in the inheritance tree of BaseClass, which defines Prop5. So the access is thanks to protected part.

    0 讨论(0)
提交回复
热议问题