我有一些 C# 代码,其中包含一个多行字符串(这是一些代码模板(,其中有一些以#if something
开头的行。这一切都工作正常,除非我将整个 C# 类包装在实际条件#if
#endif
中,在这种情况下,编译器说"无效的预处理器指令"。
代码片段:
#if WIN32
class Something {
string s = @"
#if SOMETHING
#endif";
}
#endif
有没有办法做到这一点? 我考虑过在我的代码模板中使用不同的字符,并使用替换来修复它,但这对我来说不是一个很好的解决方案,因为它使代码模板更难理解。
我会执行以下操作:
const string hash = "#";
#if WIN32
class Something {
string s = $@"
{hash}if SOMETHING
{hash}endif";
}
#endif
我想你把你的 " 和 ;在错误的地方。这工作正常,没有任何抱怨
#if DEBUG
class Something {
readonly string s = @"
#if CODE_DEFINED_ONLY_IF_SET
public class IAmOnlyHappyWhenItRains{
public int Year{get;set;}
public string Name{get;set;}
}
#endif
";
}
#endif