Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何为C#条件编译预处理器指令生成自定义StyleCop规则_C#_Visual Studio 2010_C Preprocessor_Stylecop_Conditional Compilation - Fatal编程技术网

如何为C#条件编译预处理器指令生成自定义StyleCop规则

如何为C#条件编译预处理器指令生成自定义StyleCop规则,c#,visual-studio-2010,c-preprocessor,stylecop,conditional-compilation,C#,Visual Studio 2010,C Preprocessor,Stylecop,Conditional Compilation,是否可以编写自定义的C#StyleCop规则来计算条件编译预处理器指令,如#if、#elif、#else、#endif 我们正在一个需要使用条件编译符号的环境中开发,我想在此基础上添加我们的自定义编码约定 使用CsDocument.WalkDocument-方法是否可以实现这一点,或者我是否必须使用其他方法 (FxCop在这里不起作用,因为它可以在已经编译的二进制文件上工作)。是的,这是可能的,但是CsDocument.WalkDocument重载不会有帮助,因为预处理器指令仅表示为令牌。它们没

是否可以编写自定义的C#StyleCop规则来计算条件编译预处理器指令,如#if、#elif、#else、#endif

我们正在一个需要使用条件编译符号的环境中开发,我想在此基础上添加我们的自定义编码约定

使用
CsDocument.WalkDocument
-方法是否可以实现这一点,或者我是否必须使用其他方法


(FxCop在这里不起作用,因为它可以在已经编译的二进制文件上工作)。

是的,这是可能的,但是
CsDocument.WalkDocument
重载不会有帮助,因为预处理器指令仅表示为令牌。它们没有对应的元素、语句或表达式

要为条件编译指令编写规则,需要检查CsDocument.Tokens集合。e、 g:

foreach (var directive in document.Tokens
                             .Where(t => t.CsTokenClass == CsTokenClass.ConditionalCompilationDirective)
                             .Cast<ConditionalCompilationDirective>())
{
    // ...
}
foreach(document.Tokens中的var指令
.Where(t=>t.CsTokenClass==CsTokenClass.ConditionalCompilationDirective)
.Cast())
{
// ...
}