编译时目标平台/处理器 [英] Target platform/processor at compile time
问题描述
在C#中是否有一个#define,可以让我在编译时知道是针对x86(Win32)还是x64(Win64)进行编译?
Is there a #define in C# that allows me to know, at compile time, if I'm compiling for x86 (Win32) or x64 (Win64)?
推荐答案
默认情况下,无法执行此操作。原因是C#代码不是在CLR上运行时才针对特定平台设计的。
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.
虽然可以手动滚动。您可以在Visual Studio中使用项目配置设置来定义自己的常量。或者,如果您想要更简化一点,则可以自己编辑.csproj并手动滚动一些具有各种定义的配置。
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.
例如,您可以制作您的项目文件如下图所示。我删除了一些信息,以使x86 / amd64信息更清晰。
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>
将此添加到.csproj文件中后,在项目中为我提供了2种新的平台配置。
Adding this to a .csproj file gives me 2 new platform configurations in my project.
这篇关于编译时目标平台/处理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!