[英]How to Add Custom Rule to FXCop for avoiding == operator inside the method
I want to add a Custom rule for avoiding the '==' operator inside a method in a class. 我想添加一个自定义规则,以避免在类的方法内使用'=='运算符。 For example in the below method i need to avoid 'str1==str2' with string.Compare(str1, str2,StringComparison.Ordinal);
例如在下面的方法中,我需要避免使用string.Compare(str1,str2,StringComparison.Ordinal);的 'str1 == str2' ; .So I need to check this types of code are appear in any of the method
所以我需要检查这种类型的代码是否出现在任何方法中
public void StringTest2()
{
string str1 = "Hello";
string str2 = "HELLO";
if (str1 == str2)
{
}
}
Just say no. 拒绝吧。
The string == operator already performs an ordinal comparison, and is considerably more readable IMO than insisting on using string.Compare
. 字符串==运算符已经执行了序数比较,并且比坚持使用
string.Compare
更具可读性。
Even if you did want an ordinal string comparison explicitly, I'd suggest using string.Equals(string, string, StringComparison)
instead of Compare
. 即使您确实确实希望明显地比较序数字符串,我还是建议使用
string.Equals(string, string, StringComparison)
而不是Compare
。
**The below code checks both the assignment and equal to operator in an assembly**
public override ProblemCollection Check(Member member)
{
var method = member as Method;
if (method == null)
return null;
if (method.Instructions.Count > 0)
{
foreach (var instruction in method.Instructions)
{
if (instruction != null)
if ( instruction.OpCode == OpCode.Ceq)
{
var resolution = GetResolution(member.Name.Name);
var problem = new Problem(resolution, member)
{
Certainty = 95,
FixCategory = FixCategories.Breaking,
MessageLevel = MessageLevel.Warning
};
Problems.Add(problem);
}
}
}
return base.Problems;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.