If your code contains classes, interfaces or structs that have the internal (c#) access qualifier or the Friend (VB.NET) access qualifier, you cannot access them from another assembly (e.g. unit testing). Types have this qualifier for one of two reasons

– You have explicitly marked them as internal.
– By default, top-level classes, interfaces, or structs that have no explicit access qualifier are treated as internal.

But what if you need to make them visible just for a specific assembly? Thats where the assembly InternalsVisibleTo attribute comes into play.
If your Assembly which has the Internal access qualifiers has no strong name you simply have to put the following line of code to its AssemblyInfo-File.

Sample C#

[assembly:InternalsVisibleTo("OtherAssemblyName")]

Sample VB.NET

<Assembly:InternalsVisibleTo("OtherAssemblyName")>

If your Assembly which has the Internal access qualifiers has strong name you simply have to put the following line of code to its AssemblyInfo-File.
The PublicKey depends on your Assembly, so you need to put the right PublicKey in it, check the links on the bottom of this Posts on how to get the PublicKey.

Sample C#

[assembly:InternalsVisibleTo("OtherAssemblyName,PublicKey=00240000048000009400000006020000002400005253413100040000010001002B4D3670141EEBB497143AD5400105B520B65C7CC91076C86BDEBDFB9BBB527D4B98F51F70D12B92008DB4D0715C26E7C4857CD1D44EC12AD17CED8E7DC94CD4F28E4962BB88EF6224F4BC6C1A7F35238383B70E56323B48B1C57F4E0510C0E9C5625A0A485AB22A03C7B199CB529FB234D279F883633A18509AC231B643E1E5")]

Sample VB.NET

<Assembly:InternalsVisibleTo("OtherAssemblyName,PublicKey=00240000048000009400000006020000002400005253413100040000010001002B4D3670141EEBB497143AD5400105B520B65C7CC91076C86BDEBDFB9BBB527D4B98F51F70D12B92008DB4D0715C26E7C4857CD1D44EC12AD17CED8E7DC94CD4F28E4962BB88EF6224F4BC6C1A7F35238383B70E56323B48B1C57F4E0510C0E9C5625A0A485AB22A03C7B199CB529FB234D279F883633A18509AC231B643E1E5")>

More informations: Setting the InternalsVisibleTo Attribute, Getting Public Key Token of Assembly Within Visual Studio, AssemblyHelperTool

2 thought on “How to use the assembly InternalsVisibleTo attribute”

Leave a Reply