.net - 在 Visual Studio 中构建时有条件地使用 32/64 位引用

标签 .net visual-studio 64-bit 32bit-64bit

我有一个以 32/64 位构建的项目,并具有相应的 32/64 位依赖项。我希望能够切换配置并使用正确的引用,但我不知道如何告诉 Visual Studio 使用适合体系结构的依赖项。

也许我的处理方式是错误的,但我希望能够在配置下拉列表中在 x86 和 x64 之间切换,并使引用的 DLL 具有正确的位数。

最佳答案

这是我在之前的项目中所做的,这将需要 .csproj 文件的手动版本。您还需要为不同的二进制文件提供单独的目录,最好是彼此的同级目录,并且与您的目标平台具有相同的名称。

向项目添加单个平台的引用后,在文本编辑器中打开 .csproj。前一篇<ItemGroup> <Project> 内的元素元素,添加以下代码,这将有助于确定您正在运行(和构建)的平台。

<!-- Properties group for Determining 64bit Architecture -->
<PropertyGroup>
  <CurrentPlatform>x86</CurrentPlatform>
  <CurrentPlatform Condition="'$(PROCESSOR_ARCHITECTURE)'=='AMD64' or '$(PROCESSOR_ARCHITEW6432)'=='AMD64'">AMD64</CurrentPlatform>
</PropertyGroup>

然后,针对您的平台特定引用,您可以进行如下更改:

<ItemGroup>
  <Reference Include="Leadtools, Version=16.5.0.0, Culture=neutral, PublicKeyToken=9cf889f53ea9b907, processorArchitecture=x86">
    <SpecificVersion>False</SpecificVersion>
    <HintPath>..\..\Lib\Leadtools\$(CurrentPlatform)\Leadtools.dll</HintPath>
  </Reference>
  <Reference Include="Leadtools.Codecs, Version=16.5.0.0, Culture=neutral, PublicKeyToken=9cf889f53ea9b907, processorArchitecture=x86">
    <SpecificVersion>False</SpecificVersion>
    <HintPath>..\..\Lib\Leadtools\$(CurrentPlatform)\Leadtools.Codecs.dll</HintPath>
  </Reference>
  <Reference Include="Leadtools.ImageProcessing.Core, Version=16.5.0.0, Culture=neutral, PublicKeyToken=9cf889f53ea9b907, processorArchitecture=x86">
    <SpecificVersion>False</SpecificVersion>
    <HintPath>..\..\Lib\Leadtools\$(CurrentPlatform)\Leadtools.ImageProcessing.Core.dll</HintPath>
  </Reference>
  <Reference Include="System" />
  <Reference Include="System.Core" />
  <Reference Include="System.Data.Entity" />
  <!--  Other project references -->
</ItemGroup>

注意 $(CurrentPlatform) 的使用属性,我们上面定义的。相反,您可以使用条件来确定要包含哪个平台的程序集。您可能还需要:

  • 替换$(PROCESSOR_ARCHITEW6432)$(PROCESSOR_ARCHITECTURE)$(Platform)仅考虑项目的目标平台
  • 更改平台确定逻辑以适合当前计算机,这样您就不会构建/引用 64 位二进制文​​件以在 32 位平台上执行。

我最初是为工作中的内部 Wiki 编写此内容,但是,我对其进行了修改并发布了 full process to my blog ,如果您对详细的分步说明感兴趣。

关于.net - 在 Visual Studio 中构建时有条件地使用 32/64 位引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3832552/

相关文章:

python - 来自 C (Visual Studio) 的 CMD 命令

wpf - Visual Studio 中的 Xaml 2009 支持

wpf - 解锁用于 VS2010 的 Ribbon WPF 程序集

c++ - 为什么 Win x64 中 LogonUser() 返回的 token 不属于 LOCAL 组?

C# - 如何从 static void main 调用方法

c# - .Net 中的单元测试属性?

c# - 如何从 Action 中返回值

用 mod 计算 rand 的动态范围

c++ - 生成随机 64 位整数

.net - 如何使用 Magick.net 将 ImageMagick 命令映射到 .net 代码