如何从通用字典中获取值使用自定义比较器的键? [英] How to get a value from a generic dictionary using a custom comparer for the key?

查看:137
本文介绍了如何从通用字典中获取值使用自定义比较器的键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象的通用字典,并希望使用自定义比较器来更新字典中的值。

I have a generic dictionary of objects and want to use a custom comparer to update the value in the dictionary.

myObjects 包含对象的字典,值是对象存在的次数。请注意,该值可以使用不同的比较器多次增加或完全删除。

myObjects contains a dictionary of objects and the value is the number of times that object exists. Note that the value may be incremented numerous times using different comparators or removed altogether.

testObject 是我的自定义对象。

testObject is my custom object.

customComparer 是一个基于testObject类型动态更改的比较器。但是所有比较器都是类型的 IEqualityComparer

customComparer is a dynamically changing comparer based on the type of testObject. but all comparers are of the type IEqualityComparer<MyObject>

IDictionary<MyObject, int> myObjects;
var testObject;
var customComparer;

if (myObjects.Keys.Contains(testObject, customComparer))
{
    //get the value, if its > 1 then decrement the value
    //else remove the entry entirely
    //not sure how to get the value based on my custom comparer??

    //this code below does not work because it requires the custom comparer
    //var occurrences = myObjects[testObject];
    //if (occurrences > 1)
    //    myObjects[testObject]--;
    //else
    //    myObjects.Remove(testObject);
}
else
{
    myObjects.Add(testObject, 1);
}



我可以使用Keys.Contains来确定对象是否存在与自定义比较器,但那么我不知道如何更新值。

I can use Keys.Contains to determine if the object exists with custom comparer but then i'm not sure how to update the value?

推荐答案

没有快速实现。相反,我创建了一个包装器IEqualityComparer,它包含许多内部相等比较器。包装器重写了Equals和GetHashCode方法,这些方法基于比较对象的属性选择了相等的比较器的内部字典。

There was no quick imlpementation for this. Instead, I created a wrapper IEqualityComparer that housed numerous internal equality comparers. The wrapper overwrote the Equals and GetHashCode methods which picked the appropriate internal dictionary of equality comparers based on a property from the comparison object.

public class WrapperComparer : IEqualityComparer<MyObject>
{
    private IDictionary<string, IEqualityComparer<MyObject>> _myComparerList;

    public bool Equals(MyObject x, MyObject y)
    {
        var comparer = _myComparerList[x.SomeProperty];
        return comparer.Equals(x, y);
    }

    public bool GetHashCode(MyObject obj)
    {
        var comparer = _myComparerList[obj.SomeProperty];
        return comparer.GetHashCode(obj);
    }
}



Then the comparison works...

var testObject;
var customComparer = new WrapperComparer(list of comparers here);
IDictionary<MyObject, int> myObjects = 
    new Dictionary<MyObject, int>(customComparer);

if (myObjects.ContainsKey(testObject))
{
    var occurrences = myObjects[testObject];
    if (occurrences > 1)
      myObjects[testObject]--;
    else
      myObjects.Remove(testObject);
}
else
{
    myObjects.Add(testObject, 1);
}

这篇关于如何从通用字典中获取值使用自定义比较器的键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆