Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InternalsVisibleTo with "private protected"

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

  • reside in the same assembly and
  • derive from the defining class.

(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).

like image 461
Bernhard Hiller Avatar asked Dec 01 '17 10:12

Bernhard Hiller


People also ask

What is private protected?

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.

What's the usage of InternalsVisibleTo attribute?

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”.


1 Answers

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.

like image 184
Rune Avatar answered Sep 29 '22 16:09

Rune