根据进程位数使用 32 位或 64 位特定 dll

发布于 2024-12-06 10:15:28 字数 585 浏览 0 评论 0原文

我需要引用一个有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

欲拥i 2024-12-13 10:15:28

从我的角度来看,最直接但不太灵活的方法是使用 Condition 在 csproj 文件中显式指定特定于平台的引用:

<ItemGroup Condition=" '$(Platform)' == 'x86' ">
    <Reference Include="MyAssemblyx86">

您也可以使用 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:

<ItemGroup Condition=" '$(Platform)' == 'x86' ">
    <Reference Include="MyAssemblyx86">

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

全部不再 2024-12-13 10:15:28

请参阅处理答案对于 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文