【问题标题】:Loading different assemblies at compile-time based on Build Target根据构建目标在编译时加载不同的程序集
【发布时间】:2009-01-29 05:09:38
【问题描述】:

我想根据构建目标加载四个单独的 C# 程序集之一。这将进入一个带有 .net framework 3.0 的 web 服务。

可能性:

32 位调试:AmtApiWrapper32d.dll

32 位版本:AmtApiWrapper32.dll

64 位调试:AmtApiWrapper64d.dll

64 位版本:AmtApiWrapper64.dll

这些包装器是一个单独的 C++ 项目,它包装了我编写的 C Native DLL。 C/C++ 是我常用的平台,所以如果这是一个小问题,请原谅。

所有包装 DLL 都包含完全相同的函数和原型。除了这个之外,它们还用于许多其他目的,所以除非这真的很糟糕,否则设置保持不变。

所以,我想在编译时加载其中一个。我查看了反射、GetDelegateForFunctionPointer 和其他一些东西,它们看起来都很相似,但对于这个简单的任务来说过于复杂。有什么建议么? 谢谢

【问题讨论】:

    标签: reflection c#-3.0


    【解决方案1】:

    这绝对是可能的,但你必须涉足构建文件。

    你会想要这样的:

    <ItemGroup Condition=" '$(Configuration)' == '32-bit debug' ">
      <Reference Include="AmtApiWrapper32d">
    </ItemGroup>
    <ItemGroup Condition=" '$(Configuration)' == '32-bit release' ">
      <Reference Include="AmtApiWrapper32">
    </ItemGroup>
    <ItemGroup Condition=" '$(Configuration)' == '64-bit debug' ">
      <Reference Include="AmtApiWrapper64d">
    </ItemGroup>
    <ItemGroup Condition=" '$(Configuration)' == '64-bit release' ">
      <Reference Include="AmtApiWrapper64">
    </ItemGroup>
    

    我建议你无条件地让其中一个工作,然后看看参考到底是什么样的。

    如果您想将其添加为构建时参考。如果你想在 P/Invoke 声明中使用它,只需在适当的属性周围使用 #if SYMBOL/#endif

    【讨论】:

    • 关闭!谢谢乔恩。这里: Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' 等
    【解决方案2】:

    您是否尝试过使用预编译器指令并在构建目标中定义不同的变量?

    #ifdef 32bdebug
    <load dll here>
    #endif
    

    自从我使用 C# 以来已经有一段时间了,但这应该在编译时工作。这是 C# 对 C 和 C++ 的一种借鉴。

    【讨论】:

    • 谢谢,在 C/C++ 中,您可以在 ifdef 中使用 LoadLibrary(),但我需要 C# 版本。 Jon Skeet 的建议似乎是最干净的方法。谢谢
    猜你喜欢
    • 2013-12-03
    • 2013-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-06
    • 1970-01-01
    相关资源
    最近更新 更多