[英]InternalsVisibleTo for dynamically generated assembly, but with strong naming
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. 此代理类使用项目的内部类(因此不公开实现细节),因此我将InternalsVisibleTo与我动态生成的程序集的名称一起使用。 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. 出现这个问题的原因是,为了将InternalsVisibleTo与强名称程序集一起使用,它引用的程序集也必须是强名称的,并且必须提供公钥。 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 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? 有没有人知道如何签署动态生成的程序集,以便可以通过InternalsVisibleTo访问它? I can just make the necessary classes public, but that ends up leaking implementation details that would be better left encapsulated. 我可以公开必要的类,但最终会泄漏实现细节,最好留下封装。
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. MSDN上有一个“ 如何:使用完全签名为动态程序集提供强名称 ”文章,该文章显示了如何使用Reflection.Emit对生成的程序集进行签名。
StrongNameKeyPair kp;
// Getting this from a resource would be a good idea.
using(stream = GetStreamForKeyPair())
{
kp = new StrongNameKeyPair(fs);
}
AssemblyName an = new AssemblyName();
an.KeyPair = kp;
AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.RunAndSave);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.