A new version of the .Net framework and C# offer a new access modifier: private protected
. In order to access such a member, the class must both
(In contrast to protected internal
where fulfilling one of the conditions is enough)
For testing purposes, the InternalsVisibleTo
attribute comes in very handy when I like to access non-public members of a class from my test class which is in a different assembly.
How does private protected
interact with the InternalsVisibleTo
attribute? Can I access such member from a class residing in the "friend" assembly which derives from the original class?
(I cannot try that on my machine, because the version of Visual Studio and C# is too old).
The private protected keyword combination is a member access modifier. A private protected member is accessible by types derived from the containing class, but only within its containing assembly. For a comparison of private protected with the other access modifiers, see Accessibility Levels.
From the documentation, the assembly-level InternalsVisibleTo attribute: Specifies that all nonpublic types in an assembly are visible to another assembly. This attribute was introduced in C# 2.0, and allows you to specify other assemblies that can see all types and members marked “internal”.
Yes, classes in you friendly test-assembly that derives from your base-class will get access to private protected
members.
The proposal for the new access modifier is explicit in saying what CLR access specifier it maps to (protectedAndInternal
), but does not make any note about how this in turns relates to InternalVisibleTo
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With