如何使用#if来决定在C#中编译哪个平台

Mic*_*son 7 c# 32bit-64bit preprocessor-directive

在C++中有预定义的宏:

#if defined(_M_X64) || defined(__amd64__)
    // Building for 64bit target
    const unsigned long MaxGulpSize = 1048576 * 128;// megabyte = 1048576;
    const unsigned long MaxRecsCopy = 1048576 * 16;
#else
    const unsigned long MaxGulpSize = 1048576 * 8;// megabyte = 1048576;
    const unsigned long MaxRecsCopy = 1048576;
#endif
Run Code Online (Sandbox Code Playgroud)

这允许我设置常量来控制将使用的内存量.

当然我可以逐字定义预处理器变量:

#define Is64bit 1

using System;
using System.Collections.Generic;
Run Code Online (Sandbox Code Playgroud)

-后来-

#if Is64bit
    // Building for 64bit target
    const long MaxGulpSize = 1048576 * 128;// megabyte = 1048576;
    const long MaxRecsCopy = 1048576 * 16;
#else
    const long MaxGulpSize = 1048576 * 8;// megabyte = 1048576;
    const long MaxRecsCopy = 1048576;
#endif
Run Code Online (Sandbox Code Playgroud)

我找不到基于配置管理器中设置的值来检测平台的方法,这将允许命令行构建:

set de=C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe
set sol=E:\Some\Path\to\my.sln
"%de%" %sol% /build "Release|x86"
"%de%" %sol% /build "Release|x64"
Run Code Online (Sandbox Code Playgroud)

有没有办法检测到这个或者我是否需要构建,更改平台并再次构建?

Joh*_*ner 12

您可以将任何常量添加到.csproj文件中.这些可以放入有条件的属性组中,如下所示.

 <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
    <DefineConstants>TRACE;X64</DefineConstants>
    ...
 </PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

对于我的Release x64版本,我已经定义了一个X64常量,我可以这样使用:

#if X64

#endif
Run Code Online (Sandbox Code Playgroud)