相关疑难解决方法(0)

C#中不同文件的预处理程序指令

我知道我可以使用预处理器指令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)
  1. 我究竟做错了什么?
  2. 在不同文件中使用预处理器的正确方法是 C#什么?
  3. 有没有更好的方法呢?

c# conditional-compilation c-preprocessor

10
推荐指数
1
解决办法
7929
查看次数