0

I have something link this:

public abstract class Wrapper<T, TWrapped>: where TWrapped : Wrapper<T, TWrapped>
{
   protected T baseObject;
   protected ICollection<T> baseList;
   protected ICollection<TWrapped> wrappedList;  
   public Wrapper (T base, ICollection<T> baseList, ICollection<TWrapped> wrappedList) { }
}

Then when I derive from it I need to to something like:

public class Base { }
public class Sample: Wrapper<Base, Sample> { }

Is there a way to remove the TWrapped and create a reference to the derived type? I tried using ICollection<Wrapped<T>> but then I remember that there is no covariance in ICollection.

EDIT: Clarifications, what I want with this wrapper is provide removal funcionality (and some other things) within the object (I can't change the base object so I need a wrapper to give this funcionality and manipulate it). This abstract class will have methods like this:

void Remove()
{
   while(this.baseList.Remove(baseObject));
   this.baseList = null;
   while(this.wrappedList.Remove((TWrapped)this));
   this.wrappedList = null;
}
Luiz Borges
  • 537
  • 1
  • 5
  • 19
  • 1
    Sorry, I'm missing the point. Perhaps you should explain _why_ you have arrived at this... contraption and perhaps how you would normally use it. I have the distinct feeling there is at least one unnecessary layer of complexity here (and otherwise the layers need to be separated for clarity) – sehe Apr 30 '11 at 18:14
  • What is the class trying to achieve? ie how are you going to use it? – BrandonAGr Apr 30 '11 at 18:16
  • I already solved my problem. I changed the way I aproached it. I will post an answer with the solution later (I'm a new user, I can't post an anwser to myself before 8 hours). – Luiz Borges Apr 30 '11 at 22:18

1 Answers1

0

I end up changing the logic of how I'm going to make the lists sync and allow Items to remove themselves. I created a new class to hold a collection of the wrapped items:

public interface IWrapper<TModel>
{
    TModel Model { get; }
}

public class WrapperCollection<TWrapper, TModel> : ObservableCollection<TWrapper> where TWrapper : IWrapper<TModel>
{
    protected IList<TModel> modelList;

    public ReadOnlyObservableCollection<TWrapper> AsReadOnly { get; private set; }

    protected WrapperCollection(IList<TModel> modelList)
    {
        this.modelList = modelList;
        AsReadOnly = new ReadOnlyObservableCollection<TWrapper>(this);           
    }

    public WrapperCollection(IList<TModel> modelList, Func<TModel, TWrapper> newWrapper)
        :this(modelList)
    {
        foreach (TModel model in modelList)
            this.Items.Add(newWrapper(model));
    }

    public WrapperCollection(IList<TModel> modelList, Func<TModel, WrapperCollection<TWrapper, TModel>, TWrapper> newWrapper)
        : this(modelList)
    {
        foreach (TModel model in modelList)
            this.Items.Add(newWrapper(model, this));
    }

    protected override void ClearItems()
    {
        modelList.Clear();
        base.ClearItems();
    }

    protected override void InsertItem(int index, TWrapper item)
    {
        modelList.Insert(index, item.Model);
        base.InsertItem(index, item);
    }

    protected override void RemoveItem(int index)
    {
        modelList.RemoveAt(index);
        base.RemoveItem(index);
    }

    protected override void SetItem(int index, TWrapper item)
    {
        modelList[index] = item.Model;
        base.SetItem(index, item);
    }
}

Using the sample class:

public class wrappedInt: IWrapper<int>
{
    private WrapperCollection<wrappedInt, int> list;
    public Model { get; private set; }
    public wrappedInt(int source, WrapperCollection<wrappedInt, int> list)
    {
        this.Model = source;
        this.list = list;
    }
    public void RemoveMe()
    {
        if (list != null)
        {
            list.Remove(this);
            list = null;
        }
    }
}

Then I can instantiate a collection with new WrapperCollection<wrappedInt, int>(listOfInts, (model, parent) => new wrappedInt(model, parent));.

Luiz Borges
  • 537
  • 1
  • 5
  • 19