A C# test project must call internal helpers in a strong-named class library; how do you grant access with InternalsVisibleTo, and what can still fail?
answer
- an assembly-level attribute in the library
- simple name of the friend assembly
- signed library needs the full public key
- private stays private
basics
~20 sAdd [assembly: InternalsVisibleTo("Billing.Core.Tests")] to the library, or an InternalsVisibleTo item in its project. A strong-named library must name the test assembly with its full public key (CS1726), and the test assembly must be signed with that key (CS0281).
solid answer
~40 s`InternalsVisibleTo` is an assembly-level attribute placed in the **granting** library; it names the friend by its simple assembly name, and the SDK can generate it from `<InternalsVisibleTo Include="Billing.Core.Tests" />` in the csproj. The friend then sees `internal` types and members, `protected internal` members through any reference, and `private protected` members from its derived classes; `private` members stay out of reach. Adding a version to the name is CS1725. If the library is strong-named, the attribute must carry `PublicKey=` with the friend's full public key, not a token, or the library itself fails with CS1726; and if the test assembly's key does not match (for example it is unsigned), the test build fails with CS0281.
code
plaintext · 5 lines# Billing.Core signed with key.snk, InternalsVisibleTo names only "Billing.Core.Tests":
error CS1726: Friend assembly reference 'Billing.Core.Tests' is invalid. Strong-name signed assemblies must specify a public key in their InternalsVisibleTo declarations.
# InternalsVisibleTo now carries PublicKey=..., but Billing.Core.Tests is not signed:
error CS0281: Friend access was granted by 'Billing.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e5fb51ff187b8dc7', but the public key of the output assembly ('') does not match that specified by the InternalsVisibleTo attribute in the granting assembly.go deeper
Recall that InternalsVisibleTo goes in the library and names the test assembly, and that it opens internal members, never private ones.
Explain the friend table: internal, protected internal and private protected from derived classes, and how the SDK item generates the attribute.
Diagnose the strong-name failures by number: CS1726 on the library for a missing public key, CS0281 on the friend for a key mismatch, CS1725 for a version in the name.
Keep the friend list to test assemblies; each production friend is an undeclared consumer of internals that every refactoring must now consider.
## What the attribute does `System.Runtime.CompilerServices.InternalsVisibleToAttribute` is applied to an **assembly**, in the library that owns the internals. It makes the named assembly a **friend**: when the C# compiler builds the friend, it treats the library's `internal` declarations as if they were declared in the friend's own assembly. The attribute allows multiple instances, so one library can name several friends, one attribute each. ```csharp using System.Runtime.CompilerServices; [assembly: InternalsVisibleTo("Billing.Core.Tests")] ``` In an SDK-style project the same attribute can be generated from an item, which keeps it next to the other build settings: ```xml <ItemGroup> <InternalsVisibleTo Include="Billing.Core.Tests" /> </ItemGroup> ``` The SDK's assembly-info target turns each item into an attribute, adding `PublicKey=` when the item has `PublicKey` metadata or the project sets a `PublicKey` property. ## What a friend can and cannot reach | Library member | Friend's ordinary code | Friend's derived class | |---|---|---| | `internal` type or member | yes | yes | | `protected internal` | yes, through any reference | yes | | `private protected` | no | **yes** | | `protected` | no | through its own type only | | `private` | no | no | Two details matter in practice: - **`private` is never opened.** The attribute widens the assembly boundary, not the type boundary. A test that tries to read a private field of the library's class does not even find it: the SDK reports CS1061 ("does not contain a definition"), because members the friend cannot access are not imported from the reference. - **A non-friend sees the same CS1061 for an internal method**, while an internal *type* is reported as CS0122. Either way, that is the signal the attribute is missing or does not match. ## Naming the friend correctly The string is the friend's **simple assembly name**, which by default is the test project's file name. Things that break it: 1. **A version or culture in the string.** `"Billing.Core.Tests, Version=1.0.0.0"` is CS1725: "InternalsVisibleTo declarations cannot have a version, culture, public key token, or processor architecture specified." 2. **A renamed test assembly.** If the test project sets `<AssemblyName>` to something else, the attribute no longer matches and every internal reference fails to compile. ## Strong naming changes both sides If the library is **strong-named** (signed with a key), the compiler enforces matching identities: - The library's attribute must include the friend's **full public key**, as a `PublicKey=` hex string. A name-only attribute makes the *library* fail with CS1726, and so does a `PublicKeyToken=` in place of the key. - The friend must be signed with the key whose public half appears in the attribute. An unsigned test project then fails with CS0281, whose message shows the friend's public key as `''`. Both diagnostics are printed in the examples. The usual fix is to sign the test project with the same key file as the library (or with a separate test key whose public key goes into the attribute) and to keep that key in the repository alongside the projects that need it. ## A checklist when the friend build fails 1. Read the diagnostic number first: CS1726 and CS1725 are reported on the **library**, CS0281 on the **friend**. 2. Compare the attribute string with the friend's actual assembly name, including any `<AssemblyName>` override. 3. If the library is signed, confirm the attribute holds the friend's full public key and that the friend is signed with the matching key. 4. If one member still fails with CS1061 or CS0122, check whether it is `private`, or `protected` and reached through a base-typed variable, which no friend grant changes. ## When it is the right tool - **Unit tests for internal logic** that would be clumsy to reach through the public surface: parsers, rounding rules, state machines. - **Not for production assemblies.** Granting a sibling production assembly access to internals couples the two releases: every internal change becomes a potential break for that friend, without any compiler warning on the library side. - **Not a security control.** It controls what the C# compiler lets the friend's source code name; it does not encrypt or hide anything from reflection. Some code analysis rules also change behavior once a friend exists; for example, the rules that suggest sealing or removing unused internal types assume a friend might use them. That interaction is a reason to keep the friend list short.
- Does InternalsVisibleTo let the friend override an internal virtual method?Yes, if the type is accessible and not sealed: a derived class in the friend sees the internal virtual member as accessible and can override it. That is one reason not to name production assemblies as friends: the library then has an unseen subclass relying on an internal member.
- The test project compiles but reports CS1061 for a member that is clearly in the library; what do you check first?Whether the member is `private`, which no friend can reach, and whether the friend grant applies at all: the attribute string must equal the test assembly's simple name, and for a strong-named library it must carry the matching full public key. An inaccessible member is not imported, so the compiler reports it as missing rather than as protected.
- Why must the attribute carry the full public key rather than the public key token?For a strong-named library the compiler checks the friend's full public key, and a token is only a hash of it. On a signed library both a name-only string and a `PublicKeyToken=` string fail with CS1726; once the full key is in place, a friend signed with a different key, or not signed, fails with CS0281.
A friend grant is a guest list kept by the host, not a pass carried by the guest: the library writes the test assembly's exact name on its own list, and a signed library also checks the guest's full ID, its public key, at the door.
- Puts the InternalsVisibleTo attribute in the test project instead of the library.
- Expects InternalsVisibleTo to expose private members to tests.
- Uses the public key token in the attribute for a strong-named library.
- Adds the test assembly's version number to the attribute string.
- Grants production assemblies friend access as a shortcut instead of designing a public API.