【问题标题】:C# Generic Objects CompareingC# 通用对象比较
【发布时间】:2014-08-26 07:14:02
【问题描述】:

这是一个通用类,用于将两个对象与用户定义的属性进行比较,但它不能正常工作。

public class ObjectComparer<T> : IEqualityComparer<T>
{
    private string _propertyName;

    private string PropertyName
    {
        get { return _propertyName; }
        set
        {
            _propertyName = value;
            PropertyInfo = typeof(T).GetProperty(PropertyName);
        }
    }

    public ObjectComparer(string propertyName)
    {
        PropertyName = propertyName;
    }

    private PropertyInfo PropertyInfo { get; set; }

    public int GetHashCode(T type)
    {
        if (type== null)
        {
            return 0;
        }
        return PropertyInfo.GetHashCode();
    }

    public bool Equals(T obj1, T obj2)
    {
        if (ReferenceEquals(obj1, obj2))
        {
            return true;
        }
        if (ReferenceEquals(obj1, null) || ReferenceEquals(obj2, null))
        {
            return false;
        }
        return PropertyInfo.GetValue(obj1, null) == PropertyInfo.GetValue(obj2, null);
    }
}

问题 1)type == null(值类型可能与 null 进行比较)

问题2)在这个位置使用ReferenceEquals是否正确?

更新

“单词”类:

public class Word
{
    public string EnglishText { get; set; }
    public string LocalText { get; set; }
    public string EditedText { get; set; }
}

用法:

var except = Program.Localizer.DefaultLanguage.Words.Except(CurrentSelectedLanguage.Words, new ObjectComparer<Word>("EnglishText"));

基于“EnglishName”的不同对象的数量不正确。


类真实和修改如下。 特别感谢 Sriram Sakthivel

public class ObjectComparer<T> : IEqualityComparer<T>
{
    private string _propertyName;

    private string PropertyName
    {
        get { return _propertyName; }
        set
        {
            _propertyName = value;
            PropertyInfo = typeof(T).GetProperty(PropertyName);
        }
    }

    public ObjectComparer(string propertyName)
    {
        PropertyName = propertyName;
    }

    private PropertyInfo PropertyInfo { get; set; }

    public int GetHashCode(T obj)
    {
        if (ReferenceEquals(obj, null))
        {
            return 0;
        }
        return PropertyInfo.GetHashCode();
    }

    public bool Equals(T obj1, T obj2)
    {
        if (ReferenceEquals(obj1, obj2))
        {
            return true;
        }
        if (ReferenceEquals(obj1, null) || ReferenceEquals(obj2, null))
        {
            return false;
        }
        return Equals(PropertyInfo.GetValue(obj1, null),
                             PropertyInfo.GetValue(obj2, null));
    }
}

【问题讨论】:

  • 你能解释一下无法正常工作是什么意思吗?

标签: c# generics compare


【解决方案1】:

您的GetHashCode 错误。您正在调用 PropertyInfo.GetHashCode 它应该使用 Property 值。

public int GetHashCode(T obj)
{
    if (object.ReferenceEquals(obj, null))
    {
        return 0;
    }
    var value = PropertyInfo.GetValue(obj);
    return value == null ? 0 : value.GetHashCode();
}

您还可以使用 object.ReferenceEquals 摆脱来自 resharper 的 可能的值类型与 null 比较警告

GetHashCode参数被命名为type,有误导,所以我把它改名为obj

更新:

如果您需要使用值比较,则必须使用 Object.Equals 方法,而不是使用 == 运算符作为 object 类型

public bool Equals(T obj1, T obj2)
{
    if (ReferenceEquals(obj1, obj2))
    {
        return true;
    }
    if (ReferenceEquals(obj1, null) || ReferenceEquals(obj2, null))
    {
        return false;
    }
    return object.Equals(PropertyInfo.GetValue(obj1, null), 
                         PropertyInfo.GetValue(obj2, null));
}

【讨论】:

  • 不工作是什么意思?发生什么了?否则我们不知道出了什么问题。
  • 这段代码运行正常。我没注意。非常感谢您的帮助。
猜你喜欢
  • 2011-07-09
  • 1970-01-01
  • 1970-01-01
  • 2011-04-01
  • 2010-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多