开发者

Referencing a derived type inside itself

开发者 https://www.devze.com 2023-03-02 13:05 出处:网络
I have something link this: public abstract class Wrapper<T, TWrapped>: where TWrapped : Wrapper<T, TWrapped>

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&开发者_如何学JAVAgt; 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;
}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消