Target platform/processor at compile time

后端 未结 3 2019
鱼传尺愫
鱼传尺愫 2021-01-12 06:40

Is there a #define in C# that allows me to know, at compile time, if I\'m compiling for x86 (Win32) or x64 (Win64)?

相关标签:
3条回答
  • 2021-01-12 07:01

    By default there is no way to do this. The reason is that C# code is not designed to target a particular platform as it runs on the CLR.

    It is possible to hand roll this though. You can use the project configuration settings in Visual Studio to define your own constants. Or if you want it a little more streamline you can edit the .csproj yourself and hand roll some more configurations which have various defines.

    For instance you can make your project file look like the following. I removed some of the information to make the x86/amd64 information clear.

      <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
        <!-- ... -->
        <DefineConstants>TRACE;DEBUG;X86</DefineConstants>
      </PropertyGroup>
      <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|amd64' ">
        <!-- ... -->
        <DefineConstants>TRACE;DEBUG;AMD64</DefineConstants>
        <ErrorReport>prompt</ErrorReport>
        <WarningLevel>4</WarningLevel>
      </PropertyGroup>
    

    Adding this to a .csproj file gives me 2 new platform configurations in my project.

    0 讨论(0)
  • 2021-01-12 07:03

    Not that I know of. You would have to declare your own in different project configurations.

    0 讨论(0)
  • 2021-01-12 07:04

    As far as I know Visual Studio defines only DEBUG and TRACE constants. Instead of declaring such constant manually in the project configurations you could use NANT to build your project. It can determine the build platform at compile time and define a custom directive accordingly.

    0 讨论(0)
提交回复
热议问题