我有以下对象,我想要一个字典有条件地确定是否有重复。例如,在一个字典中,我只关心两个属性对于我的密钥是唯一的。在第二个字典中,我希望所有属性对于密钥都是唯一的。
问题1:
我应该覆盖哪些接口才能实现此目的? (例如GetHashCode,IEqualityComparer,等于运算符)
问题2:
如果我更改最终会更改密钥值的属性,我该怎么办?如果我做一个字典,这可能更相关,因为.NET框架以某种方式为我处理这个,但我从未想过它。
代码
public class EventData : IEqualityComparer<EventData>
{
public string ComputerName { get; set; }
public Guid? CategoryName { get; set; }
public string LogName { get; set; }
public int EventID { get; set; }
public long? EventUniqueTracker { get; set; }
public DateTime LastQueryDate { get; set; }
public DateTime? DateOfRecord { get; set; }
//public int QueryCount { get; set; }
public int QueryCount = 0 ;//
public string zData { get; set; }
public EventData(string computerName, Guid? categoryName, string logName, int eventID, long? eventUniqueTracker, int queryCount)
{
ComputerName = computerName;
CategoryName = categoryName;
LogName = logName;
EventID = eventID;
EventUniqueTracker = eventUniqueTracker;
LastQueryDate = DateTime.Now;
QueryCount = queryCount;
}
public EventData()
{
}
public override int GetHashCode()
{
return GetHashCode(HashType.ZCompCatLogEventAllData);
}
public object GetString(HashType hType)
{
switch (hType)
{
case HashType.AComputerName:
return ComputerName;
break;
case HashType.BCompAndCat:
return new { A = ComputerName, B = CategoryName };
break;
case HashType.CCompCatLog:
return new { A = ComputerName, B = CategoryName, C = LogName };
break;
case HashType.DCompCatLogEvent:
return new { A = ComputerName, B = CategoryName, C = LogName, D = EventID };
break;
case HashType.ECompCatLogEventUserDefined1:
case HashType.FCompCatLogEventUserDefined2:
case HashType.ZCompCatLogEventAllData:
return new { A = ComputerName, B = CategoryName, C = LogName, D = EventID, E = EventUniqueTracker };
default:
break;
}
return new object { };
}
public int GetHashCode(HashType hType)
{
return GetString(hType).GetHashCode();
return 1;
}
public override string ToString()
{
return ComputerName + " " + CategoryName + " " + LogName + " " + EventID + " " + EventUniqueTracker;
}
public bool Equals(EventData x, EventData y)
{
return x.ComputerName == y.ComputerName &&
x.CategoryName == y.CategoryName &&
x.LogName == y.LogName &&
x.EventID == y.EventID &&
x.EventUniqueTracker == y.EventUniqueTracker;
}
public int GetHashCode(EventData obj)
{
EventData ci = (EventData)obj;
// http://stackoverflow.com/a/263416/328397
return new { A = ci.ComputerName, B = ci.CategoryName, C = ci.LogName, D = ci.EventID, E = ci.EventUniqueTracker }.GetHashCode();
}
}
答案 0 :(得分:2)
听起来你应该在IEqualityComparer<EventData>
内实现EventData
- 但不是。创建两个单独的实现 - 一个用于第一个概念,一个用于第二个。然后将您的词典创建为:
var first = new Dictionary<EventData, string>(new PartialDataEqualityComparer());
var second = new Dictionary<EventData, string>(new FullDataEqualityComparer());
或者您可能希望将第二种情况视为EventData
的“自然”相等,在这种情况下,您可以使EventData
实现IEquatable<EventData>
并且在创建时不指定比较器第二本字典。
基本上,您实现IEquatable<T>
说“此类型的实例能够将自身与T
的实例进行比较”,而您实现IEqualityComparer<T>
说“这种类型的实例能够比较T
”的任何两个实例。
基本上,你被塞满了。您不会(或者至少可能不会)在您的词典中再次找到该密钥。你应该尽可能小心地避免这种情况。就个人而言,我通常会发现那些适合字典键的类也是不可变的候选者。如果我更改最终会更改密钥值的属性,我该怎么办?