如何声明好友程序集? [英] How to declare a friend assembly?

查看:16
本文介绍了如何声明好友程序集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的解决方案中有 2 个项目:

I have 2 projects in my solution:

  1. 程序集(基本库)
  2. 测试程序集 (NUnit)

我已在第一个项目中将测试程序集声明为朋友程序集:

I had declared the test assembly as friends assembly in first project:

[assembly: InternalsVisibleTo ("Company.Product.Tests")]

一切正常,直到我意识到我忘记设置解决方案来签署我的程序集.所以创建了一个snk文件并设置visual studio项目来签署第一个程序集(基本库).现在,当我编译第一个项目时,出现以下错误:

Everything was working fine till I realised that I have forgot to setup the solution to sign my assemblies. So created a snk file and setup the visual studio project to sign the first assembly (Basic Library). Now when I compile the first project, I get following error:

朋友集会参考'Company.Product.Tests' 无效.强名称签名程序集必须在他们的指定一个公钥InternalsVisibleTo 声明.

Friend assembly reference 'Company.Product.Tests' is invalid. Strong-name signed assemblies must specify a public key in their InternalsVisibleTo declarations.

我尝试使用 sn 实用程序从我的 snk 文件中提取公钥,但它生成了一个有线二进制文件,我不确定如何使用.我该如何解决问题?

I tried to extract the public key from my snk file using sn utility but it generates a wired binary file which I am unsure how to use. How can I fix the problem?

推荐答案

您需要对两个程序集进行签名,因为实际上这两个程序集相互引用.

You need to sign both assemblies, because effectively both assemblies reference each other.

您必须将公钥放在 InternalsVisibleTo 属性中.例如,在我使用的协议缓冲区中:

You have to put the public key in the InternalsVisibleTo attribute. For example, in Protocol Buffers I use:

[assembly:InternalsVisibleTo("Google.ProtocolBuffers.Test,PublicKey="+
"00240000048000009400000006020000002400005253413100040000010001008179f2dd31a648"+
"2a2359dbe33e53701167a888e7c369a9ae3210b64f93861d8a7d286447e58bc167e3d99483beda"+
"72f738140072bb69990bc4f98a21365de2c105e848974a3d210e938b0a56103c0662901efd6b78"+
"0ee6dbe977923d46a8fda18fb25c65dd73b149a5cd9f3100668b56649932dadd8cf5be52eb1dce"+
"ad5cedbf")]

通过运行获取公钥

sn -Tp path	o	estassembly.dll

或者,从 .snk 文件中获取:

Alternatively, get it from the .snk file:

sn -p MyStrongnameKey.snk public.pk
sn -tp public.pk

这篇关于如何声明好友程序集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆