InternalsVisibleTo attribute isn't working
Are you absolutely sure you have the correct public key specified in the attribute? Note that you need to specify the full public key, not just the public key token. It looks something like:
[assembly: InternalsVisibleTo("MyFriendAssembly,
PublicKey=0024000004800000940000000602000000240000525341310004000001000100F73
F4DDC11F0CA6209BC63EFCBBAC3DACB04B612E04FA07F01D919FB5A1579D20283DC12901C8B66
A08FB8A9CB6A5E81989007B3AA43CD7442BED6D21F4D33FB590A46420FB75265C889D536A9519
674440C3C2FB06C5924360243CACD4B641BE574C31A434CE845323395842FAAF106B234C2C140
6E2F553073FF557D2DB6C5")]
It's 320 or so hex digits. Not sure why you need to specify the full public key - possibly with just the public key token that is used in other assembly references it would be easier for someone to spoof the friend assembly's identity.
Another possible "gotcha": The name of the friend assembly that you specify in the InternalsVisibleToAttribute
must exactly match the name of your friend assembly as shown in the friend's project properties (in the Application tab).
In my case, I had a project Thingamajig
and a companion project ThingamajigAutoTests
(names changed to protect the guilty) that both produced unsigned assemblies. I duly added the attribute [assembly: InternalsVisibleTo( "ThingamajigAutoTests" )]
to the Thingamajig\AssemblyInfo.cs file, and commented out the AssemblyKeyFile
and AssemblyKeyName
attributes as noted above. The Thingamajig
project built just fine, but its internal members stubbornly refused to show up in the autotest project.
After much head scratching, I rechecked the ThingamajigAutoTests
project properties, and discovered that the assembly name was specified as "ThingamajigAutoTests.dll". Bingo - I added the ".dll" extension to the assembly name in the InternalsVisibleTo
attribute, and the pieces fell into place.
Sometimes it's the littlest things...
If your assemblies aren't signed, but you are still getting the same error, check your AssemblyInfo.cs file for either of the following lines:
[assembly: AssemblyKeyFile("")]
[assembly: AssemblyKeyName("")]
The properties tab will still show your assembly as unsigned if either (or both) of these lines are present, but the InternalsVisibleTo attribute treats an assembly with these lines as strongly signed. Simply delete (or comment out) these lines, and it should work fine for you.