简体   繁体   English

c#预处理器指令范围

[英]c# Pre-processor directive scope

I'm looking to use: 我想用:

#define

and

#if

to allow me to simulate potentially absent hardware during unit tests. 允许我在单元测试期间模拟可能缺少的硬件。 What are the rules for using the #define statements? 使用#define语句有哪些规则?

ie what is its default scope? 即它的默认范围是什么? can I change the scope of the directive? 我可以改变指令的范围吗?

As Chris said, the scope of #define is just the file. 正如克里斯所说,#define的范围只是文件。 (It's worth noting that this isn't the same as "the class" - if you have a partial type, it may consist of two files, one of which has symbol defined and one of which doesn't! (值得注意的是,这与“类”不同 - 如果你有一个部分类型,它可能包含两个文件,其中一个有符号定义,其中一个没有!

You can also define a symbol project-wide, but that's done with project properties or a compiler switch rather than being specified in source code. 您还可以在项目范围内定义符号,但这是使用项目属性编译器开关完成的,而不是在源代码中指定。

MSDN ,其范围是文件

虽然你不能沿着模拟物体的路线走下去 ,ala Mock.Rhinos

Yes as Chris mentioned, its scope is the whole file. 是的,正如克里斯提到的,它的范围是整个文件。 You can use the defined keyword anywhere in the file. 您可以在文件中的任何位置使用已定义的关键字。

ie; 即;

#define something
... some code ...

and in any method, class body or namespace, you could use it like; 在任何方法,类主体或命名空间,你可以使用它;

#if something
  ... some conditional code ...
#else
  ... otherwise ...
#endif

The scope of a preprocessor directive starts when it's parsed from the source and persists until directed otherwise. 预处理程序指令的范围从它从源解析后开始,并持续到其他方向。 If you do want to limit the scope of a preprocessor directive, use the "undef" declaration it switch off when your done with it. 如果您确实想限制预处理程序指令的范围,请使用它完成时关闭的“undef”声明。

#include <iostream>
using namespace std ;
int main()
{
  #define someString "this is a string"
  cout<<someString<<endl;
  #undef someString  // scope of someString ends here
  cout<<someString<<endl; //this causes a compile error
  return 0 ;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 快速问题:C#中的此预处理程序指令出了什么问题 - Quick Question: Whats wrong with this pre-processor directive in C# C#中#if DEBUG预处理器指令的用法是什么? 什么时候必须使用它? - What is the usage of #if DEBUG pre-processor directive in C#? When must we use this? C# 源代码生成器:通过命令行属性或预处理器定义常量有条件地生成代码 - C# Source Generators: conditionally generate code via command line properties or pre-processor define constants 根据 C# 中的条件语句创建预处理器指令 - Create Pre Processor Directive based on conditional statement in C# 从 C# 调用 C 预处理器时,UTF-8 输入文件(作为参数提供)的某些字符已损坏 - While calling C pre-processor from C#, some characters of a UTF-8 input file (supplied as argument) are corrupted 在C#/ Visual Studio 2010中构建标签。在AssemblyInfo.cs中创建可由预处理器/编译器使用的常量 - Build labelling in C# / Visual Studio 2010. Create a constant that can be used by pre-processor / compiler in AssemblyInfo.cs 消除预处理器命令的方法 - Way to eliminate pre-processor commands 在一个项目中定义预处理器标志,并在另一个项目中使用它 - Define pre-processor flag in one project and use it in another 预处理程序指令是否可以保护客户端的服务器代码? - Do pre-processor directives protect server code from the client? C#中的#line processor指令增加了行号 - #line processor directive in C# increases the line number
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM