是否可以将ConditionalWeakTable与元组键(或包含多个引用的键)一起使用? [英] Is it Possible to Use ConditionalWeakTable with a Tuple Key (or a key comprised of multiple references)?

查看:197
本文介绍了是否可以将ConditionalWeakTable与元组键(或包含多个引用的键)一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ConditionalWeakTable的忠实粉丝.真的很棒.本质上,它使您可以在线程安全的情况下将一个引用与另一个引用关联/附加/配对.

I am a big fan of the ConditionalWeakTable. It's really quite great. It essentially allows you associate/attach/pair one reference to another reference, all while being thread-safe.

它为我回答了很多问题.不幸的是,我似乎仍然遇到一个问题,无法找到一个好的答案.

It answers a lot of questions/problems for me. Unfortunately, I seem to have a problem I continue to run into, and have not been able to find a good answer to it.

假设我有一个方法(由下面的测试方法表示),其中我使用的是ConditionalWeakTable,但不是将单个引用用作键,而是需要组合两个项并将其制成键.这是通过Key对象(出于演示目的和简洁性的目的,它只是扩展的Tuple)在下面显示的:

Let's say I have a method (denoted by a test method below) where I am using a ConditionalWeakTable, but instead of using a single reference for the key, I instead need to combine two items and make that the key. This is shown below by way of a Key object (which is simply an extended Tuple for demonstration purposes and terseness):

[Fact]
public void TupleKey()
{
    var first = new object();
    var table = new ConditionalWeakTable<Key, object>();
    var context = table.GetValue( new Key( first, new object() ), key => new object() );

    // .. table has one entry here.

    GC.Collect();
    GC.WaitForPendingFinalizers();

    Debugger.Break(); // Table is empty here.

}

class Key : Tuple<object, object>
{
    public Key( object item1, object item2 ) : base( item1, item2 ) {}
}

调用Debugger.Break时,即使在测试方法中有对Key部分之一的有效引用,ConditionalWeakTable也为空.这是有道理的,因为在测试中找不到对Key的引用.

When the Debugger.Break is called, the ConditionalWeakTable is empty, even though there is an active reference to one of the parts of the Key in the test method. This makes sense as the reference to the Key is nowhere to be found in the test.

但是,由于存在一个对Key对象一部分之一的活动引用,因此我希望ConditionalWeakTable中的条目也保持活动状态,并保持活动状态,直到其所有子级/内部引用都被激活为止.声称.

However, since there is one active reference to one of the parts of the Key object, I would like the entry in the ConditionalWeakTable to also remain active, and stay active until all of its children/inner references are claimed.

(在考虑此问题和可能的解决方案时,如果有WeakHashSet可以以某种方式用作Dictionary<>的键,那会很棒,但是通过某些搜索,它确实似乎WeakHashSet在.NET中不存在.)

(In thinking about this and possible solutions, it would awesome if there was a WeakHashSet that could somehow be used as the key to a Dictionary<>, but from some searching, it does not appear that a WeakHashSet exists in .NET.)

希望这概述了我的困境.我基本上想使用ConditionalWeakTable(或某些等效项)存储关联的值(作为键/值条目),但我不想使用一个(弱)引用作为条目的键,使用两个(或多个).

Hopefully this outlines my dilemma. I am basically wanting to use a ConditionalWeakTable (or some equivalent) to store an associated a value (as an key/value entry), but rather than using one (weak) reference as the key to the entry, I would like to use two (or more).

我正在寻求实现的可能(就是不必编写大量的自定义代码)吗?

Is what I am looking to achieve possible (without having to write a significant amount of custom code, that is)?

推荐答案

此问题与

This question is related to this question and I have finally found an answer that solves this one and that one was well. You can read the answer for more details.

关于这个问题.归结为对ConditionalWeakTable使用的键"的更精细/更聪明/更智能的使用.就我而言,我正在努力处理键的三个部分(实例,方法和参数).我设法通过使用Delegate(基本上是实例和方法的引用元组)组合了前两个,然后将其与使用Dictionary<object[], object>配对. com/questions/3609823/what-problem-does-istructuralequable-and-istructuralcomparable-solve/5601068#5601068> StructuralEqualityComparer .鲸鱼.问题解决了.

In regards to this question. It comes down to a more refined/clever/intelligent use of the "key" to use for a ConditionalWeakTable. In my case, I was wrestling with three parts of a key (instance, method, and arguments). I managed to combine the first two by use of a Delegate (which is basically a reference tuple to the instance and method) and then pair that with a Dictionary<object[], object> that uses a StructuralEqualityComparer. Whalah. Problem solved.

这篇关于是否可以将ConditionalWeakTable与元组键(或包含多个引用的键)一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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