开发者

How to use ICloneable<T> when T is List<T>?

开发者 https://www.devze.com 2023-03-25 07:07 出处:网络
I have the following: public class InstanceList : List<Instance> {} I would like to make this cloneable. Followi开发者_Go百科ng the example here: Why no ICloneable<T>?

I have the following:

    public class InstanceList : List<Instance> {}

I would like to make this cloneable. Followi开发者_Go百科ng the example here: Why no ICloneable<T>?

I tried the following:

    public interface ICloneable<T> : ICloneable Where T : ICloneable<T>
           {        new T Clone();    }

    public class InstanceList : List<Instance>, ICloneable<List<Instance>>  {}

But I get a compiler error. The error message states that List<Instance> must be convertible to ICloneable<List<Instance>> in order to use parameter T in the generic interface ICloneable<T>.

What am I missing here?


You can't do this, because you can't define List<T> yourself. You would only be able to do this if you could declare your own List<T> because of the way you've constrained ICloneable<T>. Since List<T> truly doesn't implement ICloneable<T>, you're going to have to have the type of T be InstanceList instead, which you do have control over.

Here's how you would implement it:

public class InstanceList : List<Instance>, ICloneable<InstanceList>
{
    public InstanceList Clone()
    {
        // Implement cloning guts here.
    }

    object ICloneable.Clone()
    {
        return ((ICloneable<InstanceList>) this).Clone();
    }
}

public class Instance
{

}

public interface ICloneable<T> : ICloneable where T : ICloneable<T>
{
    new T Clone();
}

Of course, there is another alternative you could do. You could widen your generics a little bit, to create a CloneableList<T> type:

public class CloneableList<T> : List<T>, ICloneable<CloneableList<T>>
{
    public CloneableList<T> Clone()
    {
        throw new InvalidOperationException();
    }

    object ICloneable.Clone()
    {
        return ((ICloneable<CloneableList<T>>) this).Clone();
    }
}

public interface ICloneable<T> : ICloneable where T : ICloneable<T>
{
    new T Clone();
}

And if you really want to get fancy, create something that restricts T to ICloneable. Then you could implement ICloneable on the Instance class, and anything else you want to include in an ICloneable<T> list, thus treating every CloneableList<T> in the exact same way, avoiding a different implementation of ICloneable<T> for each and every cloneable list you want to create.

public class CloneableList<T> : List<T>, ICloneable<CloneableList<T>> where T : ICloneable
{
    public CloneableList<T> Clone()
    {
        var result = new CloneableList<T>();
        result.AddRange(this.Select(item => (T) item.Clone()));
        return result;
    }

    object ICloneable.Clone()
    {
        return ((ICloneable<CloneableList<T>>) this).Clone();
    }
}

public interface ICloneable<T> : ICloneable where T : ICloneable<T>
{
    new T Clone();
}


The problem is your generic constraint where T : IClonable<T>. Because you're "instantiating" your interface as ICloneable<List<Instance>>, List<Instance> is your T, and so the generic constraint translates to where List<Instance> : IClonable<List<Instance>>. List<Instance> does not fulfill that constraint.

Perhaps you're trying to do something like this:

public interface ICloneableList<T> : ICloneable where T : ICloneable
{
}


To add to the other good answers already there - when you clone, you expect to get an identical copy back, right? So instead of:

public class InstanceList : List<Instance>, ICloneable<List<Instance>>  {}

Shouldn't it actually be:

public class InstanceList : List<Instance>, ICloneable<InstanceList>  {}

That way you will also get no compiler errors.


I don't think you can really do what you want. While it is useful not to require the type argument of ICloneable<T> to implement ICloneable<T>, I don't think the List<T> class can be very well extended to support cloning since it does not provide any means of detaching or duplicating the array which holds all the data items, does not allow a subclass access to that array, and does not allow a subclass to override enough virtual methods to render the array irrelevant. Although clone methods should start by using MemberwiseClone (to ensure that the cloned object is the same type as the original), there would be no guaranteed way to force the newly-cloned list to create a new array to hold its objects without disturbing the old one.

The closest thing I can suggest to doing what you want would be to define an ICloneableList<T> which inherits from IList<T> and ICloneable<IList<T>> and define an CloneableList class which implementats that by wrapping a list. Cloning a CloneableList should create a new List<T> with items copied from the old one, which can be done by using the appropriate constructor for the new List.

0

精彩评论

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

关注公众号