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

发布于 2024-10-16 14:42:00 字数 1096 浏览 10 评论 0原文

我有一个使用动态代码生成来创建代理类的项目。该代理类利用项目的内部类(以便不暴露实现细节),因此我将InternalsVisibleTo 与动态生成的程序集的名称一起使用。直到最近,当我的客户强行要求所有交付的程序集都必须是强名称时,这种方法一直运行良好。

出现此问题的原因是,为了将 InternalsVisibleTo 与强名称程序集一起使用,它引用的程序集也必须是强名称的,并且您必须提供公钥。我遇到困难的是如何为动态生成的程序集提供强名称。这是我到目前为止所做的事情:

  1. 我为动态程序集创建了一个新的密钥对,以便 .snk 可以随产品一起提供(显然我们不想提供用于签名的 .snk)项目程序集的其余部分。)
  2. 我已提取了 PublicKey 并更新了我的 InternalsVisibleTo 以将新的动态 PublicKey 用于动态引用的程序集。
  3. 我尝试像这样对动态生成的程序集进行签名:

     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:

  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.)
  2. I've extracted the PublicKey and updated my InternalsVisibleTo to use the new dynamic PublicKey for dynamically referenced assemblies.
  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? I can just make the necessary classes public, but that ends up leaking implementation details that would be better left encapsulated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

前事休说 2024-10-23 14:42:00

有一个“如何:使用完整签名为动态程序集指定一个强名称< /a>”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);

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.

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文