我想添加一个自定义规则来避免类中方法中的'=='运算符。 例如,在下面的方法中,我需要避免'str1 == str2'与 string.Compare(str1,str2,StringComparison.Ordinal); 。所以我需要检查这种类型的代码出现在任何方法
中public void StringTest2()
{
string str1 = "Hello";
string str2 = "HELLO";
if (str1 == str2)
{
}
}
答案 0 :(得分:1)
说不。
string == operator已经执行了序数比较,并且比坚持使用string.Compare
的IMO更具可读性。
即使您 明确要求进行序数字符串比较,我建议您使用string.Equals(string, string, StringComparison)
代替Compare
。
答案 1 :(得分:0)
**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;
}