[英]is there a tool to propagate preprocessor directives?
假设我有以下 class 定义:
class foo
{
#if bar
private bool bar;
#endif
public void Do()
{
bar = false;
}
}
有没有办法将预处理器指令包装 bar 传播到使用 bar 的每个地方。 output 应该是这样的:
class foo
{
#if bar
private bool bar;
#endif
public void Do()
{
#if bar
bar = false;
#endif
}
}
不,基本上。 但是,您可以将代码拆分为两个文件并使用partial class
以便您的所有bar
都在一个独立的文件中:
partial class foo
{
partial void OnBar(bool value);
public void Do()
{
OnBar(false);
}
}
和
#if bar
partial class foo
{
private bool bar;
partial void OnBar(bool value)
{
bar = value;
}
}
#endif
现在主文件对bar
一无所知。 如果未定义bar
编译符号,则bar
字段不存在, OnBar
方法也不存在 - 该方法及其调用简单地消失了。
这在许多情况下都很有用,包括额外级别的调试代码,或针对多个平台/框架/操作系统(具有针对不同目标的特定文件) - 无需使用#if
填充您的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.