[英]How to identify a Method in fxcop
I am trying the create a custom fxcop rule which checks for all the methods in target assembly having their names NOT starting with the CAPITAL letter. 我正在尝试创建一个自定义fxcop规则,该规则检查目标程序集中具有其名称不是以CAPITAL字母开头的所有方法。 I am pretty successful in doing this but there is one problem.
我这样做很成功,但是有一个问题。 The rule throws error for "delegate methods" as well, for ex.
例如,该规则也会对“代理方法”引发错误。 btnOk_Click which I don't want, is there any way to identify/filter delegate methods in fxcop using any predefined property/method ?
我不想要的btnOk_Click,是否可以使用任何预定义的属性/方法在fxcop中标识/过滤委托方法?
An idea would be to write custom code rules through the tool NDepend instead (Disclaimer: I am one of the developer of the tool). 一个想法是改为通过工具NDepend编写自定义代码规则(免责声明:我是该工具的开发人员之一)。
NDepend is especially conceived to make easy custom code rules edition through LINQ query. NDepend是专门通过LINQ查询来简化自定义代码规则版本的构想。 The following Code Query LINQ (CQLinq) query covers your need:
以下代码查询LINQ (CQLinq)查询满足您的需求:
// <Name>Method name MUST start with CAPITAL</Name>
warnif count > 0
from m in Application.Assemblies.WithName("TargetAssemblyName").ChildMethods()
where
!m.IsSpecialName && // Remove getter and setter
!m.IsGeneratedByCompiler && // Discard methods generated by compiler
!m.ParentType.IsDelegate &&
!m.NameLike("^btn") && // Use regex here to discard btnOk_Click like method
!char.IsUpper(m.SimpleName[0])
select m
Just write this code rule in NDepend query editor in VS, and get an immediate feedback: 只需在VS的NDepend查询编辑器中编写此代码规则,即可获得立即反馈:
NDepend code rule can be executed/validated live in VS , or can be executed at Build Process time and validated in a report . NDepend代码规则可以在VS中实时执行/验证 ,或者可以在“构建过程”时执行并在报告中进行验证 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.