【问题标题】:Is it possible to programmatically perform an assembly redirect是否可以以编程方式执行程序集重定向
【发布时间】:2013-06-04 21:39:49
【问题描述】:

我们在一些新的类库中使用 Microsoft 的 Unity 框架进行依赖注入。主应用程序正在使用其他一些需要 Unity v2.0.414 的企业库(Common 和 Logging),但我们使用 Unity v2.1.515 实现了我们的库。

为了解决版本差异,我为主应用程序创建了一个 app.Config,并将一个 bindingRedirect 条目放入配置文件中,效果很好。然而,我们刚刚了解到,到目前为止,该应用程序从未使用过 app.config,并且 mgmt 更喜欢这种方式。

那么是否可以以编程方式实现程序集重定向(即在代码中)?谢谢!

【问题讨论】:

    标签: .net unity-container app-config


    【解决方案1】:

    您尝试过AppDomain.AssemblyResolve 事件吗?

    这篇文章也可以帮助你How to use Assembly Binding Redirection to ignore revision and build numbers

    public void Load(string assembly)
    {
        AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(Method);
        Assembly assembly = Assembly.LoadFrom(assemblyFile);
        // ...
    }
    

    【讨论】:

    • 这很好用,谢谢!我检查 args.Name 以查看是否包含“microsoft.practices.unity”,如果包含,则返回 typeof(UnityContainer).Assembly 否则返回 null - 再次感谢您的帮助!
    • 这是为 C# cmdlet 绑定程序集的方法。它们不作为 exe 运行,因此不使用自己的 app.config。如果需要重定向绑定,则必须在 BeginProcessing() 覆盖中添加上述事件。
    猜你喜欢
    • 1970-01-01
    • 2011-10-17
    • 1970-01-01
    • 2012-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-31
    • 2013-06-29
    相关资源
    最近更新 更多