Hei*_*bug 10 c# conditional-compilation c-preprocessor
我知道我可以使用预处理器指令C#来启用/禁用某些代码的编译.
如果我在同一个文件中定义一个指令,它可以正常工作:
#define LINQ_ENABLED
using System;
using System.Collections.Generic;
#if LINQ_ENABLED
using System.Linq;
#endif
Run Code Online (Sandbox Code Playgroud)
现在,我用于C++将所有这些配置指令放在单个头文件中,并将其包含在我需要这些指令的所有文件中.
如果我做同样的C#事情是行不通的:
//Config.cs
#define LINQ_ENABLED
//MyClass.cs
#define LINQ_ENABLED
using System;
using System.Collections.Generic;
#if LINQ_ENABLED
using System.Linq;
#endif
Run Code Online (Sandbox Code Playgroud)
我也试过以下但似乎我无法在命名空间内定义指令:
//Config.cs
namespace Conf{
#define LINQ_ENABLED
}
//MyClass.cs
#define LINQ_ENABLED
using System;
using System.Collections.Generic;
using Conf;
#if LINQ_ENABLED
using System.Linq;
#endif
Run Code Online (Sandbox Code Playgroud)
C#什么?Syn*_*der 10
在你的.csproj中有一节:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DefineConstants>TRACE;DEBUG;LINQ</DefineConstants>
</PropertyGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)
如果您需要额外的预处理器,可以在那里添加它们.
或者通过项目的属性自动为您添加它们.在"构建"选项卡下的属性中.