views:

169

answers:

2

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. 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.


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.

+4  A: 

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:

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>.


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

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. 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.

Aaronaught
Actually I don't think I fully thought this through. The idea was that they could pass in a `SortedList` or `Queue` or what have you, if they needed their collection to have certain properties, but I just realized that they wouldn't have access to any of the additional methods that some of those collections have anyway... so either of your solutions would probably work fine. Unless I could inherit from TCollection though, I think my class will be pointless :\
Mark
W/ your 2nd sol'n... you can't instantiate an `ICollection`... however, I could force the programmer to pass in a collection to the c'tor I suppose, as Matt points out.
Mark
Nice update! I don't think I want to head down either of those routes at the moment though... I'll just write a couple custom collections that fit my need for now :)
Mark
+1  A: 

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.

Would that work for you?

Matt Hamilton