在其内部引用派生类型

发布于 2024-11-03 23:41:13 字数 920 浏览 1 评论 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> 但后来我记得 ICollection 中没有协方差。

编辑:澄清一下,我想要这个包装器是在对象内提供删除功能(以及其他一些东西)(我无法更改基本对象,所以我需要一个包装器来提供此功能并对其进行操作)。这个抽象类将具有如下方法:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回梦 2024-11-10 23:41:13

我最终改变了如何使列表同步并允许项目自行删除的逻辑。我创建了一个新类来保存包装项目的集合:

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(listOfInts, (model,parent) => newwrappedInt(模型,父级));

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文