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

前端 未结 2 1223
被撕碎了的回忆
被撕碎了的回忆 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条回答
  •  旧时难觅i
    2021-01-31 15:33

    To use InternalsVisibleTo with strongly signed assembly your "friends" assemblies must be strongly signed too. Public token of the test assembly need to be specified as part of InternalsVisibleTo value.

    Note that the attribute is not used for actual validation of assembly at compile time - it only specifies that run-time checks (and compile-time checks for friend's assembly) should validate that identity. So if you just need to compile your main assembly you can specify any public key token (i.e. one from Microsoft's assemblies as found on all assembly references in your Web.Config for example).

    Generally since you'll be signing assemblies you'd know the public key. I.e. if you have snk file than sn -t youSnk.snk would show the public key. Or you can follow steps in Getting Public Key Token of Assembly Within Visual Studio to configure your VS to show public token for any assembly which uses sn -Tp {path to assembly} to get public key from the assembly. (If document is gone steps are copied to the other answer to this question)

提交回复
热议问题