了解ConditionalWeakTableConditionalWeakTable

2023-09-03 13:44:05 作者:唯念、那抹少年蓝

我想了解 ConditionalWeakTable 。在

class ClassA
{
    static readonly ConditionalWeakTable<ClassA, OtherClass> OtherClassTable
        = new ConditionalWeakTable<ClassA, OtherClass>();
}

class ClassB
{
    OtherClass otherClass;
}

?什么是使用ClassA的或ClassB的引用可为空字段的优点和缺点是什么?

? What would be the pros and cons of using ClassA or ClassB to reference a nullable field?

推荐答案

我完全不明白你问什么我想虽然你问是否应该使用在你的type属性或 ConditionalWeakTable ,您可以在其中附加于特定类型的实例这样的属性。如果是这样,你可以交替问你是否应该单独使用属性对字典,其中可能包含在特定键这个属性(这将是您的特定类型的实例)。除非你需要这样一本词典,这是pretty的废话。

I don't exactly get what you're asking about, I assume though you're asking whether you should use property inside your type or ConditionalWeakTable, to which you can attach such a property for that particular type instance. If so, you could alternatively ask whether you should use property alone versus dictionary, which could contain this property under specific key (which will be your particular type instance). Unless you need such a dictionary, it's pretty nonsense.

理解 ConditionalWeakTable&LT; TKEY的,TValue&GT;

Understanding of ConditionalWeakTable<TKey, TValue>:

什么 ConditionalWeakTable 实际上做的是,它可以让你附加额外信息现有的,管理的,非动态的CLR对象。本质上,它可以被理解只是作为一个字典,其中两个的键和值是弱引用,并且只要键是活着的值被保持活着。更多信息,请 MSDN 被发现。

What ConditionalWeakTable actually do is, it allows you to attach additional information to existing, managed, non-dynamic CLR objects. Essentially it can be understand just as a dictionary, where both the keys and the values are weakly referenced, and a value is kept alive as long as the key is alive. More information can be found on MSDN.

所以,你应该问问自己,你有什么需要。假设你的类型的实例:

So, you should ask yourself what are your needs. Assuming your types are instantiated:

var classA = ClassA(); 
var classB = ClassB(); 
var other = OtherClass();

你想使用绑定到这样的情况下以这种方式属性:

do you want to use the property binded to such instances in this manner:

/* set */
var other = new OtherClass();        
ClassA.OtherClassTable.Add(classA, other);
/* get */
OtherClass data = null;
var result = ClassA.OtherClassTable.TryGetValue(classA, out data);

而不是这个下面?

instead of this one below?

/* set */
classB.OtherClass = other;
/* get */
var result = classB.OtherClass;

除非有特殊的需要,答案似乎是pretty的明显。当然也有进一步的问题在这里:

Unless have particular needs, the answer seems to be pretty obvious. There are of course further issues here:

什么是弱引用和为什么要使用它?

这 MSDN 不久的文章解释了话题。它基本上说弱引用不延长对象的生命周期,允许它被垃圾收集,一旦这样的对象仍然可以通过该应用程序code到达。弱引用可用于指向这应该是适用于GC,如果他们不积极使用的对象是有用的。但是,如果程序使用大量的小物件,弱引用可产生负面的内存使用情况的影响。如这和this还应该澄清一些存在的疑问。

This MSDN article shortly explains the topic. It basically says weak references do not extend the lifetime of the object, by allowing it to be garbage collected, once such an object can still be reached by the application code. Weak references can be useful for pointing to objects which should be available for GC if they are not actively in use. However, if the program uses large number of small objects, weak references can negatively affect memory usage. Threads like this and this should also clarify some remaining doubts.

如果你正在寻找一个例子,当你可以使用 ConditionalWeakTable&LT; TKEY的,TValue&GT; 在标准词典&LT; TKEY的,TValue&GT; ,想象下面的案例。你想绑定属性的字典和实例在运行,但同时不想$ P $的,如果你已经主动停止使用他们所收集pvent他们。不幸的是标准的做法是不可能的 - GC被阻止,因为字典中仍然占据着他们强大的参考,像这样的:

If you're looking for an example, when you could use ConditionalWeakTable<TKey, TValue> over standard Dictionary<TKey, TValue>, imagine the following case. You'd like to bind a dictionary of properties to and instance at runtime, but at the same time do not want to prevent them from being collected if you've stopped actively using them. Unfortunately in standard approach it's impossible - GC is blocked because dictionary still holds a strong references to them, like this:

var x = new object();
x.Props().Y = "hello";

static class ExpandoExtensions 
{
    private static IDictionary<object, dynamic> props = 
        new Dictionary<object, dynamic>();
    public static dynamic Props(this object key)
    { 
        dynamic o;
        if (!props.TryGetValue(key, out o)){
            o = new ExpandoObject();
            props[key] = o;
        }
        return o;       
    } 
}

当然,你可以随时手动拿下他们,但并不是如下简单的这种做法?

Of course you can always take down them manually, but isn't this approach shown below simpler?

static class ExpandoExtensions
{
    private static readonly ConditionalWeakTable<object, ExpandoObject> props =
        new ConditionalWeakTable<object, ExpandoObject>();

    public static dynamic Props(this object key)
    { 
        return props.GetOrCreateValue(key);       
    } 
}

同时( MSDN )

避免使用弱引用作为自动解决方案,以记忆   管理的问题。相反,制定一个有效的缓存策略   处理应用程序的对象。

avoid using weak references as an automatic solution to memory management problems. Instead, develop an effective caching policy for handling your application's objects.

上面显示这些扩展方法,从这个thread.

These extension methods shown above are taken from this thread.

相关推荐