简体   繁体   English

InternalsVisibleTo用于动态生成的程序集,但具有强大的命名功能

[英]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: 这是我到目前为止所做的:

  1. 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.) 我已经为动态装配创建了一个新的密钥对,因此.snk可以随产品一起提供(显然我们不希望运送.snk用于签署其余的项目组件。)
  2. I've extracted the PublicKey and updated my InternalsVisibleTo to use the new dynamic PublicKey for dynamically referenced assemblies. 我已经解压缩了PublicKey并更新了我的InternalsVisibleTo,以便为动态引用的程序集使用新的动态PublicKey。
  3. 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.

相关问题 更新动态生成的程序集 - Updating dynamically generated assembly 动态生成的程序集中捕获错误 - Error catching in dynamically generated assembly 动态生成的装配体中的扩展方法? - Extension Method in Dynamically Generated Assembly? 如何有条件地编译`InternalsVisibleTo(…)`程序集信息? - How to conditionally compile `InternalsVisibleTo(…)` assembly info? 什么是程序集的“AllInternalsVisible”参数:InternalsVisibleTo属性? - What is “AllInternalsVisible” parameter of assembly:InternalsVisibleTo attribute? 什么是[assembly:InternalsVisibleTo(“MyAssembly”)]? 声明,指令,......? - What is [assembly: InternalsVisibleTo(“MyAssembly”)]? A statement, directive, …? 如何检查部件上的InternalsVisibleTo属性? - How can I check for an InternalsVisibleTo attribute on an assembly? 为什么还会对InternalsVisibleTo引用的程序集进行签名? - Why has an InternalsVisibleTo referenced assembly also be signed? 如何将“InternalsVisibleTo”属性与强命名程序集一起使用? - How to use "InternalsVisibleTo" attribute with Strongly named assembly? 在为程序集添加强命名后,在BinaryFormatter.Deserialize上出现TargetInvocationException - TargetInvocationException on BinaryFormatter.Deserialize after i added strong naming to my assembly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM