How to distribute both 32 and 64 bit versions of t

2020-07-04 08:02发布

问题:

I have a C# library that is called by various clients (both 32-bit and 64-bit). Up to now it was compiled as AnyCPU, so there was no issues.

Recently I added a dependency to SQLite .NET library which come in both 32 and 64-bit flavors (but not AnyCPU). So, now, I have to have 2 builds - for both bitnesses.

In the past, I've seen other libraries (MS SQL Compact comes to mind) that had a scheme where a single .NET assembly would have Private\amd64 and Private\x86 folders in the folders with the appropriate native libraries in them and it would call each one as necessary.

Is this approach viable for my situation? Is there documentation on how to implement it? Are there code changes required or is this a distribution technique?

回答1:

There are several ways you can handle this. Code changes (small) are required for the first three approaches:

A. You can modify the PATH to point to the platform specific folder during application start up. Then .NET will automatically load local DLLs from that folder.

B. You can subscribe to the AssemblyResolve event and then choose the assembly based on the platform.

Check out Scott Bilias's blog post on this http://scottbilas.com/blog/automatically-choose-32-or-64-bit-mixed-mode-dlls/. Note that he ends up preferring approach A.

"In a nutshell, the solution is to trick the loader! Reference a p4dn.dll that does not exist, and use the AssemblyResolve event to intercept the load and reroute it to the correct bit size assembly."

C. Use a platform-specific set of exe.configs and the codebase element to determine assembly locations. Your setup would install the correct one based on platform.

http://msdn.microsoft.com/en-us/library/4191fzwb.aspx

D. Write two setups one for 32-bit and one for 64-bit, then only install the appropriate files for the platform.



回答2:

You can load the corresponding assembly on the fly, using System.Reflection.Assembly.Load

See: http://msdn.microsoft.com/en-us/library/system.reflection.assembly.aspx