简体   繁体   English

InternalsVisibleTo,签名和单元测试,如何使其实用?

[英]InternalsVisibleTo, Signing and Unit tests, how to make it practical?

In "C# in Depth 2nd Edition", Jon Skeet's book - which I've just read until end of part 2 -, it is mentioned in 7.7.3 that InternalsVisibleTo can also be used with signed assemblies. 在“C#in Depth 2nd Edition”中,Jon Skeet的书 - 我刚刚阅读到第2部分结尾 - 在7.7.3中提到InternalsVisibleTo也可以用于签名的程序集。 At the moment I did not use signatures at all. 目前我根本没有使用签名。 The security issue for released binaries is actually quite critical so I plan to remove completely the attribute for release assemblies using a preprocessor variable test. 已发布二进制文件的安全问题实际上非常关键,因此我计划使用预处理器变量测试完全删除发布程序集的属性。

Just for interest, how would it be practical to use signed assemblies and InternalsVisibleTo ? 只是为了感兴趣,使用签名的程序集和InternalsVisibleTo是如何实际的? In order to use InternalsVisibleTo for specifying a signed friend assembly, I need to specify its public key. 为了使用InternalsVisibleTo指定已签名的朋友程序集,我需要指定其公钥。 I have it only after compiling the friend assembly which has a dependency on the assembly under test (dynamic assembly loading and reflexion left aside, what would bloat-up coding and readability). 我只有在编译了一个依赖于被测试的组件的友元组件之后才能拥有它(动态组件加载和反射被放在一边,什么会膨胀编码和可读性)。 This sound like a chicken-egg problem requiring a bootstrap of the assembly to be tested. 这听起来像鸡蛋问题,需要测试组件的自举。 I can imagine some tricks with MSBuild and scripting to automate this. 我可以想象一些使用MSBuild和脚本来自动化的技巧。 Is there a more practical way of doing this? 有更实际的方法吗?

In case it remains so tedious I will stick to my first idea of dropping Unit Testing for the release builds (which is somewhat unsatisfying as subtle timing issues could be left untested...) 如果它仍然如此乏味,我将坚持我的第一个想法,即放弃发布版本的单元测试(这有些不令人满意,因为微妙的时序问题可能未经测试......)

Use the same key to sign all the projects in the solution, don't generate a different one for each project and on each build. 使用相同的密钥对解决方案中的所有项目进行签名,不要为每个项目和每个构建生成不同的项目。 I'd recommend having the same physical key file referenced by each project in the project properties, instead of copying it into each project. 我建议在项目属性中使用每个项目引用的相同物理密钥文件,而不是将其复制到每个项目中。

That way, all of them are associated with the same PublicKey constant. 这样,所有这些都与相同的PublicKey常量相关联。 And use a single entry, such as: 并使用单个条目,例如:

[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("UnitTests, PublicKey=<your key>")]

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

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