简体   繁体   English

C#-如何使我的程序集重命名?

[英]C# - How to make my assembly strongly named?

I'm trying to create unit tests for some of my private methods and I read that you can access them from your unit tests via [assembly: InternalsVisibleTo("DLLName")] . 我正在尝试为某些私有方法创建单元测试,并且我读到可以通过[assembly: InternalsVisibleTo("DLLName")]从单元测试中访问它们。

However, mine's not working and another solution was to add the Public Key. 但是,我的无法正常工作,另一个解决方案是添加公钥。 I tried to get the Public Key of my unit test project via sn -Tp DLLName but it is complaining that it isn't strongly named. 我试图通过sn -Tp DLLName获取我的单元测试项目的sn -Tp DLLName但是它抱怨它的名称不是很强。

How can I make it so that it is strongly named? 我怎样才能使它得到强命名? Am I using the InternalsVisibleTo the wrong way? 我是否以错误的方式使用InternalsVisibleTo

I think if you go to the properties of your project, on the side bar there should be a navigation link called "Signing". 我认为,如果要转到项目的属性,则在侧栏上应该有一个名为“签名”的导航链接。 There should be an option to sign the assembly. 应该有一个对程序集进行签名的选项。 If that is not there, then I don't think you can give it a strong name? 如果那不存在,那么我认为您不能给它起一个强大的名字吗?

Chances are your unit test assembly isn't strongly named at the moment - after all, it's a relatively unusual requirement, as the reasons for strong naming don't often apply to test assemblies. 很有可能您的单元测试程序集目前没有得到很好的命名-毕竟,这是一个相对不寻常的要求,因为强命名的原因通常不适用于测试程序集。

However, the use of InternalsVisibleTo does necessitate this - because it's a bit like you're adding a reference from the strongly-named production assembly to the test assembly, and you can't refer to a non-strongly-named assembly from a strongly-named one. 但是,使用InternalsVisibleTo 确实需要这样做-因为这有点像您要将引用从强名称的生产程序集添加到测试程序集,并且您不能从强名称的程序集引用未强名称的程序集命名一个。

So just make the test assembly strongly named in the same way as you have for the production assembly (Project Properties, Signing), give it a key (eg the same one as your production assembly) and then use sn -Tp to get the public key. 因此,只需使用与生产程序集相同的方式对测试程序集进行强命名(项目属性,签名),为其提供一个密钥(例如与生产程序集相同的密钥), 然后使用sn -Tp公开键。

All this is assuming that the reason your original InternalsVisibleTo attempt didn't work because the production assembly is strongly-named. 所有这一切都假定您原始的InternalsVisibleTo尝试失败的原因不起作用, 因为生产程序集是强命名的。 If it isn't, then signing the test assembly won't help at all, and you need to give us more information about the problem you're seeing. 如果不是这样,那么对测试程序集进行签名将完全无济于事,您需要向我们提供有关所遇到问题的更多信息。

To create and sign an assembly with a strong name using the Assembly Linker 使用Assembly Linker创建和签名具有强名称的部件

At the command prompt, type the following command: 在命令提示符处,键入以下命令:

al /out:<assembly name> <module name> /keyfile:<file name>

In this command, assembly name is the name of the assembly to sign with a strong name, module name is the name of the code module used to create the assembly, and file name is the name of the container or file that contains the key pair. 在此命令中,程序集名称是要使用强名称进行签名的程序集的名称,模块名称是用于创建程序集的代码模块的名称,文件名是包含密钥对的容器或文件的名称。 The following example signs the assembly MyAssembly.dll with a strong name using the key file sgKey.snk. 下面的示例使用密钥文件sgKey.snk对程序集MyAssembly.dll进行强名称签名。

al /out:MyAssembly.dll MyModule.netmodule /keyfile:sgKey.snk

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM