0

我想添加一个自定义规则,以避免在类的方法中使用“==”运算符。例如,在下面的方法中,我需要使用string.Compare(str1, str2,StringComparison.Ordinal);避免'str1==str2' ; .所以我需要检查这种类型的代码是否出现在任何方法中

public void StringTest2()
    {
        string str1 = "Hello";
        string str2 = "HELLO";
        if (str1 == str2)
        {
        }
    }
4

2 回答 2

1

拒绝吧。

string == 运算符已经执行了序数比较,并且在IMO 中比坚持使用string.Compare.

即使您确实想要明确地进行序数字符串比较,我也建议您使用string.Equals(string, string, StringComparison)而不是Compare.

于 2012-11-02T10:58:21.747 回答
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;
            }
于 2012-11-05T13:00:03.773 回答