时间:2019-03-17 标签:c#Pre-processordirectivescope
我希望使用:
#define
并
#if
允许我在单元测试期间模拟可能缺少的硬件。 使用#define
语句的规则是什么?
即它的默认范围是什么? 我可以更改指令的范围吗?
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?
i.e. what is its default scope? can I change the scope of the directive?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
来自 MSDN,其范围是文件
From MSDN, its scope is the file
虽然你不能沿着 Mock 对象的路线走下去,ala Mock.Rhinos 吗?
Although could you not go down the route of Mock objects, ala Mock.Rhinos ?
是的,正如克里斯提到的,它的范围是整个文件。 您可以在文件中的任何位置使用定义的关键字。
IE;
在任何方法、类体或命名空间中,您都可以像这样使用它:
Yes as Chris mentioned, its scope is the whole file. You can use the defined keyword anywhere in the file.
i.e;
and in any method, class body or namespace, you could use it like;
正如 Chris 所说,#define 的范围只是文件。 (值得注意的是,这与“类”不同 - 如果您有部分类型,它可能由两个文件组成,其中一个定义了符号,另一个没有定义!
您还可以定义一个项目范围内的符号,但这是通过 项目属性 或 编译器开关而不是在源代码中指定。
As Chris said, the scope of #define is just the file. (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.