Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ConcurrentDictionary TryUpdate相等比较器_C#_Concurrentdictionary - Fatal编程技术网

C# ConcurrentDictionary TryUpdate相等比较器

C# ConcurrentDictionary TryUpdate相等比较器,c#,concurrentdictionary,C#,Concurrentdictionary,我有一个ConcurrentDictionary,用作哈希表,它将定期刷新。ConcurrentDictionary的TValue是我创建的自定义类: public class RefreshableDictionaryValue<TValue> { private DateTime lastRefreshed; private DateTime lastAccessed; private TValue value; public override

我有一个ConcurrentDictionary,用作哈希表,它将定期刷新。ConcurrentDictionary的TValue是我创建的自定义类:

public class RefreshableDictionaryValue<TValue>
{
    private DateTime lastRefreshed;
    private DateTime lastAccessed;
    private TValue value;

    public override bool Equals(object obj)
    {
        if (obj == null)
        {
            return false;
        }

        TValue t = (TValue)obj;
        if((System.Object)t == null)
        {
            return false;
        }

        return this.Value.Equals(((RefreshableDictionaryValue<TValue>)obj).Value);
    }
    public override int GetHashCode()
    {
        return Value.GetHashCode();
    }
    ...
}
编辑: 以下是我如何进行更新:

foreach(KeyValuePair<TKey, RefreshableDictionaryValue<TValue>> entry in cacheDictionary)
{
    DateTime lastRefresh = entry.Value.LastRefreshed;
    DateTime lastAccess = entry.Value.LastAccessed;

    secondsSinceLastRefresh = Convert.ToInt32((DateTime.Now - lastRefresh).TotalSeconds);
    secondsSinceLastAccess = Convert.ToInt32((DateTime.Now - lastAccess).TotalSeconds);
    if (secondsSinceLastAccess < cacheExpirationTime)
    {
        if (secondsSinceLastRefresh > refreshInterval)
        {
            TValue updatedValue = valueGetter(entry.Key);

            bool s = cacheDictionary.TryUpdate(entry.Key, new RefreshableDictionaryValue<TValue>(DateTime.Now, lastAccess, updatedValue), entry.Value);
        }
     }
 }
foreach(缓存字典中的KeyValuePair条目)
{
DateTime lastRefresh=entry.Value.lastRefresh;
DateTime lastAccess=entry.Value.lastAccess;
secondssincelastresh=Convert.ToInt32((DateTime.Now-lastresh).TotalSeconds);
secondsSinceLastAccess=Convert.ToInt32((DateTime.Now-lastAccess).TotalSeconds);
if(secondsSinceLastAccessrefreshInterval)
{
TValue updatedValue=valueGetter(entry.Key);
bool s=cacheDictionary.TryUpdate(entry.Key,new RefreshableDictionaryValue(DateTime.Now,lastAccess,updatedValue),entry.Value);
}
}
}

TValue t=(TValue)obj
应替换为
TValue t=obj作为TValue
我尝试过(必须在类声明中添加where-TValue:class)。但是现在我在声明ConcurrentDictionary时遇到了一个错误:private ConcurrentDictionary cacheDictionary;//类型TValue必须是引用类型才能将其用作参数。我还认为演员的两种方式是equivalent@MarcinJuraszek如果他这样做了,他需要一个constraintOk,所以我将该约束添加到所有使用RefreshableDictionaryValue的类中,它现在可以工作了,但是如果TValue是int,它就不工作了。无论如何,TryUpdate现在仍然失败,所以我假设它可能没有使用Equals来进行相等比较?没关系,我刚刚意识到我是在使用另一种类型。这就解决了这个问题:TValue t=(obj作为RefreshableDictionaryValue).Value;
foreach(KeyValuePair<TKey, RefreshableDictionaryValue<TValue>> entry in cacheDictionary)
{
    DateTime lastRefresh = entry.Value.LastRefreshed;
    DateTime lastAccess = entry.Value.LastAccessed;

    secondsSinceLastRefresh = Convert.ToInt32((DateTime.Now - lastRefresh).TotalSeconds);
    secondsSinceLastAccess = Convert.ToInt32((DateTime.Now - lastAccess).TotalSeconds);
    if (secondsSinceLastAccess < cacheExpirationTime)
    {
        if (secondsSinceLastRefresh > refreshInterval)
        {
            TValue updatedValue = valueGetter(entry.Key);

            bool s = cacheDictionary.TryUpdate(entry.Key, new RefreshableDictionaryValue<TValue>(DateTime.Now, lastAccess, updatedValue), entry.Value);
        }
     }
 }