有传播预处理器指令的工具吗?
is there a tool to propagate preprocessor directives?
假设我有以下 class 定义:
class foo
{
#if bar
private bool bar;
#endif
public void Do()
{
bar = false;
}
}
有没有办法将包装 bar 的预处理器指令传播到每个使用 bar 的地方。输出应该是这样的:
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
现在 main 文件对 bar
一无所知。如果未定义 bar
编译符号,则 bar
字段不存在,并且 也不存在 OnBar
方法 - 方法 它的调用 简直蒸发了。
这在许多情况下都非常有用,包括额外级别的调试代码,或针对多个平台/框架/操作系统(针对不同目标使用特定文件)- 无需您的代码填充 #if
。
假设我有以下 class 定义:
class foo
{
#if bar
private bool bar;
#endif
public void Do()
{
bar = false;
}
}
有没有办法将包装 bar 的预处理器指令传播到每个使用 bar 的地方。输出应该是这样的:
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
现在 main 文件对 bar
一无所知。如果未定义 bar
编译符号,则 bar
字段不存在,并且 也不存在 OnBar
方法 - 方法 它的调用 简直蒸发了。
这在许多情况下都非常有用,包括额外级别的调试代码,或针对多个平台/框架/操作系统(针对不同目标使用特定文件)- 无需您的代码填充 #if
。