C# 如何声明好友程序集?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1123683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:45:12  来源:igfitidea点击:

How to declare a friend assembly?

c#visual-studio-2008assembly-signing

提问by Hemant

I have 2 projects in my solution:

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

  1. Assembly (Basic Library)
  2. Test Assembly (NUnit)
  1. 装配(基本库)
  2. 测试组件 (NUnit)

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

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

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

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:

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

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

朋友程序集引用“Company.Product.Tests”无效。强名称签名程序集必须在其 InternalsVisibleTo 声明中指定一个公钥。

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?

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

采纳答案by Jon Skeet

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

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

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

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

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

The public key is retrieved by running

通过运行检索公钥

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

Alternatively, get it from the .snk file:

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

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

回答by user95319

I think you need to put in the strong name, which would be something like "Company.Product.Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=17135d9fcba0119f". I assume Company.Product.Tests is your assembly name and 17135d9fcba0119f is the public key.

我认为您需要输入强名称,例如“Company.Product.Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=17135d9fcba0119f”。我假设 Company.Product.Tests 是您的程序集名称,而 17135d9fcba0119f 是公钥。

Another way to resolve this problem would be not to use separate assemblies. I usually put the source code and the testing code in the same assembly. I don't know if you have any special concern that you must separate them.

解决此问题的另一种方法是不使用单独的程序集。我通常将源代码和测试代码放在同一个程序集中。不知道你有没有什么特别的顾虑,一定要把它们分开。

回答by ezyuzin

You can directrly get publicKey from assembly which you interest, without magic with sn.exe

您可以直接从您感兴趣的程序集中获取 publicKey,而无需使用 sn.exe

<!-- language: c# -->
var assemblyName = Assembly.GetExecutingAssembly().GetName();
Console.WriteLine("{0}, PublicKey={1}",
    assemblyName.Name,
string.Join("", assemblyName.GetPublicKey().Select(m => string.Format("{0:x2}", m))));