如何使用程序集绑定重定向忽略修订和内部版本号 [英] How to use Assembly Binding Redirection to ignore revision and build numbers

查看:352
本文介绍了如何使用程序集绑定重定向忽略修订和内部版本号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个.NET应用程序在C#,以及对他们访问数据库的API。我希望把所有版本的API在数据库中,并让他们挑选的最高版本和内部版本号,但坚持它们是使用主要和次要号码。基本上当我引用API 1.2.3.4 我想引用读 1.2。*。* 使应用程序只是拿起 1.2.3.5 我知道我可以使用XML配置文件做到这一点。我宁愿它遵守。类似的发布策略,但与出多余的文件。我可以勉强接受的。另一个问题是所有的解决方案我看到重定向一个版本到另一个特定的版本,而不仅仅是任何版本的更新。

I have several .NET applications in C#, along with an API for them to access the database. I want to put all versions of the API in the database, and have them pick the highest revision and build number, but stick with the major and minor number they were built with. Basically when I reference API 1.2.3.4 I want the reference to read 1.2.*.* so that the applications just pick up 1.2.3.5 I see I can do this with XML config files. I'd rather have it complied in. Similar to publish policies, but with out the extra files. I could settle for that. The other issue is all solutions I see redirect one version to another specific version, not just to any version newer.

我如何做到这一点?

有人点我到一个信息源发行者策略?

Can someone point me to an informative source for publisher policy?

推荐答案

感谢<一href="http://stackoverflow.com/questions/1460271/how-to-use-assembly-binding-redirection-to-ignore-revision-and-build-numbers/1476551#1476551">leppie's使用的建议 <一href="http://msdn.microsoft.com/en-us/library/system.appdomain.assemblyresolve.aspx">AppDomain.AssemblyResolve事件中,我能够解决类似的问题。下面是我的code是这样的:

Thanks to leppie's suggestion of using the AppDomain.AssemblyResolve event, I was able to solve a similar problem. Here's what my code looks like:

    public void LoadStuff(string assemblyFile)
    {
        AppDomain.CurrentDomain.AssemblyResolve += 
            new ResolveEventHandler(CurrentDomain_AssemblyResolve);
        var assembly = Assembly.LoadFrom(assemblyFile);

        // Now load a bunch of types from the assembly...
    }

    Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
    {
        var name = new AssemblyName(args.Name);
        if (name.Name == "FooLibrary")
        {
            return typeof(FooClass).Assembly;
        }
        return null;
    }

这完全忽略了版本号和替换已加载库的任何名为FooLibrary库引用。您可以使用的AssemblyName的其他属性类,如果你想成为更加严格。 FooClass 可以是任何类的FooLibrary组装。

This completely ignores the version number and substitutes the already loaded library for any library reference named "FooLibrary". You can use the other attributes of the AssemblyName class if you want to be more restrictive. FooClass can be any class in the FooLibrary assembly.

这篇关于如何使用程序集绑定重定向忽略修订和内部版本号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆