在 C# 中创建具有许多变量的 equals 函数进行比较

本文关键字:equals 函数 比较 变量 许多 创建 | 更新日期: 2023-09-27 18:32:19

我在 C# 中有一个类函数,可以将一个对象与另一个对象进行比较。或多或少是一个 Equals 函数,但添加了一些代码。我的对象有很多变量,我想比较每个变量,如果有任何不同,请将它们添加到数据表中。现在我的函数看起来像这样:

    public bool compareSysInfo(SystemInfo expected, SystemInfo comp)
    {
        ComparePopup popup = new ComparePopup();
        bool finalResult = true;//initial assumption that they are equal
        //compares branding
        if (expected.branding != comp.branding)
        {
            finalResult = false;
            popup.addDataToTable("Branding", comp.getBranding() + "", expected.getBranding() + "");
        }
        //compares pro #
        if (expected.getPro() != comp.getPro())
        {
            finalResult = false;
            popup.addDataToTable("Pro Number", comp.getPro() + "", expected.getPro() + "");
        }

对于所有变量,它都继续这样。我想避免过多的if语句,有没有办法创建一个抽象方法并使用循环?我研究了代表,但我不确定在这种情况下如何使用它们。也许我做错了,应该做一些完全不同的事情,比如将它们添加到集合中,只使用 for 循环来比较对象。我觉得我错过了一些明显的东西。我知道任何有像这样的重复代码的地方都可以更好地实现,只是我不知道怎么做。这里的任何建议都会很棒。感谢您的帮助。

在 C# 中创建具有许多变量的 equals 函数进行比较

创建一个策略类,为每个规则继承它,并为您的类提供一个包含以下内容的列表:

public MyClass {
 readonly IEnumerable<Rule> _rules;
 public MyClass(IEnumerable<Rule> rules) {
    _rules
 }
 public bool CompareSysInfo(SystemInfo expected, SystemInfo comp) {
   // i prefer linq over loops
   var result = from r in _rules
                where !r.CheckRule(expected, comp)
                select false;
   return result.Count() > 0; // only returns true if no rule checks return false
 }
}

然后,对于每个规则检查,您实现 Rule 类(或接口)的一个实例:

public abstract class Rule {
   protected ComparePopup Popup { get; private set; }
   protected Rule(ComparePopup popup) {
     Popup = popup;
   }
   public abstract bool CheckRule(SystemInfo expected, SystemInfo comp);
}
public class BrandingRule : Rule {
   public BrandingRule(ComparePopup popup) : base(popup) { }
   public override bool CheckRule(SystemInfo expected, SystemInfo comp) {
     var result = expected.branding == comp.branding;
     if(!result)
       Popup.addDataToTable("Branding", comp.getBranding() + "", expected.getBranding() + "");
   }
}

此策略使您能够添加、删除或修改规则,以及它们独立于需要使用它们的代码执行的操作。

您可以从一个类中获取所有属性,并通过它们循环获取值并比较它们

Type type = expected.GetType();
PropertyInfo[] info = type.GetProperties();
foreach(PropertyInfo inf in info)
{
   if (info == null)
      return null; // or you can move on, based on what you need
   obj = info.GetValue(obj, null);
   //obj has the value, then you can compare them
 }

我认为这将帮助您入门。

比较所有字段和属性

public static bool MyCompare<T>(T obj1, T obj2)
{
    bool bp = obj1.GetType()
        .GetProperties()
        .All(p=>p.GetValue(obj1).Equals(obj2.GetType().GetProperty(p.Name).GetValue(obj2)));
    bool bf = obj1.GetType()
        .GetFields()
        .All(f => f.GetValue(obj1).Equals(obj2.GetType().GetField(f.Name).GetValue(obj2)));
    return bp && bf;
}
private bool compareValue<T1, T2>(T1 first, T2 second, Func<T1, T2> selector)
{
  if(selector(first) != selector(second))
    ;//do stuff
}

然后你可以做这样的事情:

public bool compareSysInfo(SystemInfo expected, SystemInfo comp)
{
  return compareValue(expected, comp, info => info.branding) &&
  compareValue(expected, comp, info => info.getPro());
}

这只是一个开始,不确定您是否喜欢这个前提并想使用它。