0

私はこれをリンクしています:

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) { }
}

それから派生するときは、次のようにする必要があります。

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

を削除TWrappedして派生型への参照を作成する方法はありますか? 使ってみICollection<Wrapped<T>>たのですが、 には共分散がないことを思い出しましたICollection

編集:明確化、このラッパーで私が望むのは、オブジェクト内で削除機能(およびその他のもの)を提供することです(ベースオブジェクトを変更できないため、この機能を提供して操作するにはラッパーが必要です)。この抽象クラスには、次のようなメソッドがあります。

void Remove()
{
   while(this.baseList.Remove(baseObject));
   this.baseList = null;
   while(this.wrappedList.Remove((TWrapped)this));
   this.wrappedList = null;
}
4

1 に答える 1

0

リストを同期させ、アイテムが自分自身を削除できるようにする方法のロジックを変更することになります。ラップされたアイテムのコレクションを保持する新しいクラスを作成しました。

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

サンプル クラスの使用:

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;
        }
    }
}

次に、コレクションをインスタンス化できますnew WrapperCollection<wrappedInt, int>(listOfInts, (model, parent) => new wrappedInt(model, parent));

于 2011-05-01T11:58:35.980 に答える