本文介绍了清单< T>不实现SyncRoot上!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家都用大量的名单。我需要遍历这个名单,所以我用已知格局。

Everyone use lot of List. I need to iterate over this list, so I use the known SyncRoot pattern.

最近我的张贴SyncRoot上应该有利于避免嵌入线程安全的(每个方法将一个私有对象锁定在不使用SyncRoot属性暴露它)。我能理解它,并部分我同意这一点。

Recently I noticed in this post that the SyncRoot should be avoided in favor of "embedded" thread-safety (each method will lock on an private object without exposing it using SyncRoot property). I can understand it, and partially I agree on that.

现在的问题是,的类没有实现SyncRoot属性,即使实现的接口,它暴露的 SyncRoot上的属性。我说这bause代码

The question is that List<T> class doesn't implements the SyncRoot property, even if implements the ICollection interface, which expose the SyncRoot property. I say this bause the code

 List<int> list = new List<int>()
 list.SyncRoot;



给我以下编译器错误:

give me the following compiler error:

错误CS0117:'System.Collections.Generic.List'不包含'SyncRoot上

...如果这是真的,我怎么可能同步类型列表℃的公共财产; T>在它迭代时?

...If this is true, how could I synchronize a public property of type List<T> when iterating over it?

推荐答案

它实际上是明确落实。

object ICollection.SyncRoot
{
    get
    {
        if (this._syncRoot == null)
        {
            Interlocked.CompareExchange(ref this._syncRoot, new object(), null);
        }
        return this._syncRoot;
    }
}

这意味着你必须转换为 ICollection的来使用它。

This means you must cast to ICollection to use it.

这篇关于清单&LT; T&GT;不实现SyncRoot上!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-26 08:37