开发者

Load x64 or x86 version of assembly appropriately based on bitness of referencing application

I've got two assemblies that both contain the same namespace and classes. One is 32 bit and named assembly_x86.dll, and the other is 64 bit and named assembly_x64.dll.

I would like to be able to build the containing application as AnyCPU and have it run without error on both 32 and 64 bit OSes. So I need it to dynamically choose the correct reference at run time depending on the bitness of the containing process. I've been hammering away at this for a while and haven't been able to come up with anything. I feel like I'm probably missing something simple.

My latest attempt was the add references to both assemblies, wire up the AssemblyResolve event and try to replace the 64 bit reference with the 32 bit should it fail to load the 64 bit version like so...

    static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
    {
        Assembly assembly = null;

        if (args.Name.Contains("assembly_x64"))
        {
                assembly = typeof(x86_alias::TypeName).Assembly;
        }

        return assembly;
    }

Which results in the following exception:

The located assembly's manifest definition does not match the assembly reference.

Any helpful tips would be very much appreciated. Thanks in advance.

In regard to some comments below I think pe开发者_高级运维rhaps I did a poor job describing my goal. Let me elaborate. So say for example there is just a single method in these assemblies I'd like to call - MyClass.MyMethod.

If I build the app with only the reference to the 32 bit assembly and install it on a 32 bit machine it works fine. Likewise, if I build it with a reference to the 64 bit assembly and install it on a 64 bit machine all is good. However, my goal is to not have to have two separate builds of the application. If I deploy to a 32 bit machine I'd like it to call MyClass.MyMethod in the 32 bit assembly, and if I deploy to a 64 bit machine I would like it to call MyClass.MyMethod in the 64 bit assembly.

If the assemblies both had the same name (and version, culture, publickeytoken) I believe it may just work and pick up the proper version. However, since the assembly names are different it does not. So this is how I've come to the conclusion that I need to swap out the reference at run time.


You could do the check of the OS as part of the installation, then deploy only the required DLL based on the OS. The ClickOnce bootstrapper for SQL Server CE does a similar thing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜