Custom threshold for CA1502

纵饮孤独 提交于 2019-12-29 07:11:53

问题


Is there any way to change the thresholds for CodeAnalysis rules?

In particular, we would like our Build to fail when a method has a code complexity of more than 20. Unfortunately, rule CA1502 has a threshold of 25:

The rule reports a violation when the cyclomatic complexity is more than 25.

Can we somehow change this?


回答1:


Yes, this is possible. Unfortunately, the only way to provide custom rule settings for a configurable rule is via a .fxcop project file, which doesn't integrate terribly seamlessly with VStudio due to the order in which the rule set and project files are processed. Basically, if you want to use both a ruleset file and the project file, you will need to include a list of all rule library files in your project file with the rules disabled. You will then be able to control whether the rules are enabled or disabled via the ruleset. Once that's all set up, you can tweak the settings for the cyclomatic complexity rule using a Settings section like the following (assuming you're OK with all the thresholds being set to 20):

<Settings>
    <Rule TypeName="AvoidExcessiveComplexity">
        <Entry Name="Warning Threshold">20</Entry>
        <Entry Name="Information Threshold">20</Entry>
        <Entry Name="Critical Warning Threshold">20</Entry>
        <Entry Name="Critical Error Threshold">20</Entry>
        <Entry Name="Error Threshold">20</Entry>
        <Entry Name="Recommended Threshold">20</Entry>
    </Rule>
</Settings>


来源:https://stackoverflow.com/questions/21678642/custom-threshold-for-ca1502

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!