InternalsVisibleTo attribute isn't working

后端 未结 19 2412
眼角桃花
眼角桃花 2020-11-27 13:51

I am trying to use the InternalsVisibleTo assembly attribute to make my internal classes in a .NET class library visible to my unit test project. For some reas

相关标签:
19条回答
  • You need to use the /out: compiler switch when compiling the friend assembly (the assembly that does not contain the InternalsVisibleTo attribute).

    The compiler needs to know the name of the assembly being compiled in order to determine if the resulting assembly should be considered a friend assembly.

    0 讨论(0)
  • 2020-11-27 13:55

    Previous answers with PublicKey worked: (Visual Studio 2015: NEED to be on one line, otherwise it complains that the assembly reference is invalid or cannot referenced. PublicKeyToken didn't worked)

    [assembly: InternalsVisibleTo("NameSpace.MyFriendAssembly, PublicKey=0024000004800000940000000602000000240000525341310004000001000100F73F4DDC11F0CA6209BC63EFCBBAC3DACB04B612E04FA07F01D919FB5A1579D20283DC12901C8B66A08FB8A9CB6A5E81989007B3AA43CD7442BED6D21F4D33FB590A46420FB75265C889D536A9519674440C3C2FB06C5924360243CACD4B641BE574C31A434CE845323395842FAAF106B234C2C1406E2F553073FF557D2DB6C5")]
    

    Thanks to @Joe

    To get the public key of the friend assembly:

    sn -Tp path\to\assembly\MyFriendAssembly.dll
    

    Inside a Developper command prompt (Startup > Programs > Visual Studio 2015 > Visual Studio Tools > Developer Command Prompt for VS2015). Thanks to @Ian G.

    Although, the final touch that made it work for me after the above was to sign my friend library project the same way the project of the library to share is signed. Since it was a new Test library, it wasn't signed yet.

    0 讨论(0)
  • 2020-11-27 13:55

    I'm writing this out of frustration. Make sure the assembly you are granting access to is named as you expect.

    I renamed my project but this does not automatically update the Assembly Name. Right click your project and click Properties. Under Application, ensure that the Assembly Name and Default Namespace are what you expect.

    0 讨论(0)
  • 2020-11-27 13:56

    It is worth noting that if the "friend" (tests) assembly it is written in C++/CLI, rather than C#/VB.Net then you need to use the following:

    #using "AssemblyUnderTest.dll" as_friend
    

    instead of a project reference or the usual #using statement. For some reason, there is no way to do this in the project reference UI.

    0 讨论(0)
  • 2020-11-27 13:56

    I had the same problem. None of the solutions worked.

    Eventually discovered the issue was due to class X explicitly implementing interface Y, which is internal.

    the method X.InterfaceMethod was unavailable, though I have no idea why.

    The solution was to cast (X as YourInterface).InterfaceMethod in the test library, and then things worked.

    0 讨论(0)
  • 2020-11-27 13:57

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

    0 讨论(0)
提交回复
热议问题