How to use “InternalsVisibleTo” attribute with Strongly named assembly?

前端 未结 2 1217
被撕碎了的回忆
被撕碎了的回忆 2021-01-31 15:04

I am using the \"InternalsVisibleTo\" attribute with an assembly to expose the internal methods/classes to my unit test project.

I now need to install that assembly int

2条回答
  •  南方客
    南方客 (楼主)
    2021-01-31 15:39


    UPDATE May 2019 : It works perfectly with Visual Studio 2019.


    To anyone that is using Visual Studio 2017, there is the latest method :

    From our beloved IDE, go to "Tools > External Tools..." and "Add" a new tool with those settings :

    • Title : Get PublicKey
    • Command : "C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.2 Tools\sn.exe" (/!\ choose the NETFX tool version that match your assembly NETFX version, here it's 4.6.2)
    • Arguments : -Tp $(TargetPath)
    • Check the "Use Output window" checkbox

    Apply/OK those changes.

    In the "Solution explorer" click on your project assembly name, and then head to "Tools > Get PublicKey". The Output window should display the (quite long) Public Key, along with the Public Token Key.

    Finally, in the project that holds the internal class (i.e. the tested project) you want to expose, open the "AssemblyInfo.cs" file and add the line :

    [assembly: InternalsVisibleTo("MyCompany.SolutionName.ProjectName, PublicKey=Paste your public key here")]

    /!\ You have to remove the line breaks from your public key.

    It worked perfectly for me, so hopefully it'll do the trick for you as well !

提交回复
热议问题