简体   繁体   English

通用收藏课?

[英]Generic collection class?

Is there anyway I can do this? 反正我能做到这一点吗?

class TSOC<TCollection, TValue> : ICollection<TValue>, INotifyCollectionChanged where TCollection : ICollection
{
    private TCollection<TValue> collection;
}

It doesn't like my definition of collection . 它不喜欢我对collection定义。 I'd prefer the definition to look like TSOC<TCollection> where the user can pass in List<int> or something, but then I need to pull out the "int" to know what interface to implement. 我希望定义看起来像TSOC<TCollection> ,用户可以在其中传递List<int>东西,但是随后我需要拉出“ int”以了解要实现的接口。


Basically I just wanted to wrap all the ICollection methods in locks and force them to run on a particular thread, but still keep all the additional functionality of the programmer's collection of choice.... I'm not sure this is possible though. 基本上,我只是想将所有ICollection方法包装在locks并强制它们在特定线程上运行,但是仍然保留程序员选择的集合的所有其他功能。...我不确定这是否可行。

How about: 怎么样:

class TSOC<TCollection, TValue> : ICollection<TValue>, INotifyCollectionChanged
    where TCollection : ICollection<TValue>
{
    private TCollection collection;
}

I think this is what you're looking for. 我认为这就是您要寻找的。 It seems like maybe you're trying to wrap a generic collection, and you want the first type of parameter to be an existing collection of the same type. 似乎您正在尝试包装通用集合,并且您希望第一种类型的参数是相同类型的现有集合。 This will give you exactly that. 这将为您提供确切的信息。

Hard to say, but you might not even need that. 很难说,但您甚至可能不需要。 Unless you really need to know the underlying type of the TCollection object, you could probably just make it a TSOC<TValue> and accept an ICollection<TValue> to wrap it with: 除非您真的需要了解TCollection对象的基础类型,否则可以将其设为TSOC<TValue>并接受ICollection<TValue>进行包装:

class TSOC<TValue> : ICollection<TValue>, INotifyCollectionChanged
{
    private ICollection<TValue> collection;
}

This is functionally the same thing, you just won't have knowledge of the actual underlying type of the ICollection<TValue> . 从功能上讲这是同一件事,只是您不了解ICollection<TValue>的实际基础类型。


Update: 更新:

Responding now to this edit: 现在响应此编辑:

Basically I just wanted to wrap all the ICollection methods in locks and force them to run on a particular thread, but still keep all the additional functionality of the programmer's collection of choice.... I'm not sure this is possible though 基本上,我只是想将所有ICollection方法包装在锁中,并强制它们在特定线程上运行,但仍保留程序员选择的集合中的所有其他功能。...我不确定这是否可能

I think you actually could achieve what you want, but perhaps in a slightly different way than you expect. 我认为您实际上可以实现所需的功能,但是可能与您预期的方式略有不同。 You don't even really need the collection, you just want a synchronized wrapper: 您甚至根本不需要收集,只需要一个同步包装器即可:

public class Synchronized<T>
{
    private readonly object sync = new Object();

    private T item;

    public Synchronized(T item)
    {
        this.item = item;
    }

    public void Execute(Action<T> action)
    {
        lock (sync)
            action(item);
    }

    public TResult Evaluate<TResult>(Func<T, TResult> selector)
    {
        lock (sync)
            return selector(item);
    }
}

Then use it as: 然后将其用作:

var sc = new Synchronized<ICollection<int>>(new List<int>());
sc.Execute(c => c.Add(3));
int count = sc.Evaluate(c => c.Count);

Now I know this isn't exactly what you want, since it doesn't actually give you an instance of ICollection<T> you can pass around. 现在我知道这并不是您想要的,因为它实际上并没有为您提供ICollection<T>的实例,您可以通过它。 But it's a clean encapsulation of the locking behaviour and hides the inner collection, so you can guarantee that every operation is "thread safe." 但这是对锁定行为的干净封装,并且隐藏了内部集合,因此您可以保证每个操作都是“线程安全的”。

If you really, really need an equivalent instance of ICollection<T> that wraps another ICollection<T> while adding thread safety, then what you're actually looking for is method interception using a library like Castle DynamicProxy. 如果确实需要一个等效的ICollection<T>实例,该实例在增加线程安全性的同时包装另一个ICollection<T> ,那么您真正要寻找的是使用诸如Castle DynamicProxy之类的库进行方法拦截。

If the consumer of your class is passing in an isntance of the collection, you may not need to specify it as part of the class' generic definition. 如果您的类的使用者正在传入集合的一个空白,则可能不需要将其指定为类的通用定义的一部分。

public class TSCO<T> : ICollection<T>, INotifyCollectionChanged 
{
    public TSCO(ICollection<T> collection)
    {
        _collection = collection;
    }

    private ICollection<T> _collection;
}

You need an object that implements ICollection<T> to construct the type, but you don't need to tell it what type that collection is explicitly. 您需要一个实现ICollection<T>的对象来构造类型,但是您无需告诉它该集合是什么类型。

Would that work for you? 那对你有用吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM