根据进程位数使用 32 位或 64 位特定 dll
我需要引用一个有 2 个版本的 DLL(一个用于 32 位,一个用于 64 位)。 我的目标是构建一个可以在 32 位和 64 位系统上运行的 Web 应用程序。
我考虑过默认引用32位程序集并使用AssemblyResolve事件加载64位版本(如果加载32位版本失败):
static void Main(string[] args)
{
AppDomain.CurrentDomain.AssemblyResolve += _AssemblyResolve;
// Try LoadAssembly ...
}
static System.Reflection.Assembly _AssemblyResolve(object sender, ResolveEventArgs args)
{
var path = string.Format(@"...\lib_x64\{0}.dll", args.Name);
return Assembly.LoadFrom(path);
}
但即使发生BadImageFormatException,_AssemblyResolve处理程序也不会被调用。还有其他方法可以实现建议的行为吗?
I need to reference a DLL which is available in 2 versions (one for 32bit and one for 64bit).
My goal is to build an web application that works on both 32 and 64 bit systems.
I thought about referencing the 32bit assembly by default and using the AssemblyResolve event to load the 64bit version (if loading the 32bit version failed):
static void Main(string[] args)
{
AppDomain.CurrentDomain.AssemblyResolve += _AssemblyResolve;
// Try LoadAssembly ...
}
static System.Reflection.Assembly _AssemblyResolve(object sender, ResolveEventArgs args)
{
var path = string.Format(@"...\lib_x64\{0}.dll", args.Name);
return Assembly.LoadFrom(path);
}
But even when a BadImageFormatException occurs, the _AssemblyResolve handler will not be called. Is there any other way to achieve the proposed behavior?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从我的角度来看,最直接但不太灵活的方法是使用
Condition
在 csproj 文件中显式指定特定于平台的引用:您也可以使用 Assembly.Load(AssemblyName) 方法重载。
参数的类型为 AssemblyName ,它公开属性 AssemblyName.ProcessorArchitecture 可以设置为 None、MSIL、X86、X64、IA64、AMD64
您还可以研究的一件事是
Publisher Policy File
功能和命令行参数/platform:processorArchitecture
Most straightforward way but less flexible from my point of view is explicitly specify platform specific references in csproj file using
Condition
:Also you can do it dynamically using Assembly.Load(AssemblyName) method overload.
Parameter is of type AssemblyName which exposes the property AssemblyName.ProcessorArchitecture which could be set to None, MSIL, X86, X64, IA64, AMD64
One thing you also could look into is the
Publisher Policy File
feature and command line argument/platform:processorArchitecture
请参阅处理答案对于 System.Data.SQLite。
我认为您提出的方法应该可行,但您需要移动 32 位版本,因此默认情况下无法找到它,因此始终为该 dll 调用
_AssemblyResolve
。这只是一个猜测。See answers for dealing with this for System.Data.SQLite.
I think your proposed method should work but you need to move the 32-bit version so it can't be found by default, so
_AssemblyResolve
is always called for that dll. That's just a guess.