InternalsVisibleTo 用于动态生成程序集,但具有强命名
我有一个使用动态代码生成来创建代理类的项目。该代理类利用项目的内部类(以便不暴露实现细节),因此我将InternalsVisibleTo 与动态生成的程序集的名称一起使用。直到最近,当我的客户强行要求所有交付的程序集都必须是强名称时,这种方法一直运行良好。
出现此问题的原因是,为了将 InternalsVisibleTo 与强名称程序集一起使用,它引用的程序集也必须是强名称的,并且您必须提供公钥。我遇到困难的是如何为动态生成的程序集提供强名称。这是我到目前为止所做的事情:
- 我为动态程序集创建了一个新的密钥对,以便 .snk 可以随产品一起提供(显然我们不想提供用于签名的 .snk)项目程序集的其余部分。)
- 我已提取了 PublicKey 并更新了我的 InternalsVisibleTo 以将新的动态 PublicKey 用于动态引用的程序集。
我尝试像这样对动态生成的程序集进行签名:
var name = new AssemblyName("ProxyBuilderAssembly"); var 属性 = new CustomAttributeBuilder[1]; 属性[0] = new CustomAttributeBuilder(typeof(AssemblyKeyFileAttribute).GetConstructor(new[] {typeof(string)}), 新对象[] {"Dynamic.snk"}); _ assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(名称,AssemblyBuilderAccess.RunAndSave,属性); _module = _ assembly.DefineDynamicModule("ProxyBuilderAssembly", "ProxyBuilderAssembly.dll");
不幸的是,这不起作用,而且我一直很难找到有关其如何工作的任何文档。有谁知道如何对动态生成的程序集进行签名,以便可以通过InternalsVisibleTo 对其进行访问?我可以将必要的类公开,但这最终会泄漏实现细节,而这些细节最好还是封装起来。
I have a project that uses dynamic code generation to create a proxy class. This proxy class makes use of internal classes of the project (so that implementation details are not exposed) and so I use InternalsVisibleTo with the name of my dynamically generated assembly. This worked fine until recently, when my client imposed the requirement that all shipped assemblies be strong-named.
The issue arises because, in order to use InternalsVisibleTo with a strong-named assembly, the assemblies it references must also be strong-named and you must provide a public key. Where I'm getting stuck is how to provide a strong name for the dynamically generated assembly. Here is what I've done so far:
- I've created a new key pair for dynamic assemblies, so that the .snk can be shipped with the product (obviously we don't want to ship the .snk being used for signing the rest of the project assemblies.)
- I've extracted the PublicKey and updated my InternalsVisibleTo to use the new dynamic PublicKey for dynamically referenced assemblies.
I've attempted to sign the dynamically-generated assemblies like this:
var name = new AssemblyName("ProxyBuilderAssembly"); var attributes = new CustomAttributeBuilder[1]; attributes[0] = new CustomAttributeBuilder(typeof(AssemblyKeyFileAttribute).GetConstructor(new[] {typeof(string)}), new object[] {"Dynamic.snk"}); _assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(name, AssemblyBuilderAccess.RunAndSave, attributes); _module = _assembly.DefineDynamicModule("ProxyBuilderAssembly", "ProxyBuilderAssembly.dll");
Unfortunately this is not working and I have been having a very hard time finding any documentation on how this should work. Does anybody know how to sign a dynamically generated assembly so that it can be given access via InternalsVisibleTo? I can just make the necessary classes public, but that ends up leaking implementation details that would be better left encapsulated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有一个“如何:使用完整签名为动态程序集指定一个强名称< /a>”MSDN 上的文章介绍了如何对使用 Reflection.Emit 生成的程序集进行签名。
There is a "How to: Use Full Signing to Give a Dynamic Assembly a Strong Name" article on MSDN that shows how to sign assemblies generated with Reflection.Emit.