克隆列表< T> [英] Cloning List<T>

查看:162
本文介绍了克隆列表< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想克隆一个列表,你只需调用:

I thought that to clone a List you would just call:

List<int> cloneList = new List<int>(originalList);

不过,我想,在我的code和我似乎得到了暗示的上面是简单地做的效果:

But I tried that in my code and I seem to be getting effects that imply the above is simply doing:

cloneList = originalList ......因为对cloneList似乎影响originalList。

cloneList = originalList... because changes to cloneList seem to be affecting originalList.

那么,什么是克隆一个列表的方式吗?

So what is the way to clone a List?

编辑:

我想到做这样的事情的:

I am thinking of doing something like this:

public static List<T> Clone<T>(this List<T> originalList) where T : ICloneable
{
    return originalList.ConvertAll(x => (T) x.Clone());
}

EDIT2:

我把深拷贝code。通过Binoj安东尼建议,并创造了这个扩展方法:

I took the deep copy code suggested by Binoj Antony and created this extension method:

public static T DeepCopy<T>(this T original) where T : class
{
    using (MemoryStream memoryStream = new MemoryStream())
    {
        BinaryFormatter binaryFormatter = new BinaryFormatter();
        binaryFormatter.Serialize(memoryStream, original);
        memoryStream.Seek(0, SeekOrigin.Begin);
        return (T)binaryFormatter.Deserialize(memoryStream);
    }
}

EDIT3:

现在,说,在列表中的项目结构。那么,什么会导致如果我叫:

Now, say the items in the List are structs. What then would result if I called?:

List<StructType> cloneList = new List<StructType>(originalList);

我是pretty的肯定比我会得到充满了新的独特的项目的列表,是否正确?

I am pretty sure than I would get a List filled with new unique items, correct?

推荐答案

您可以使用下面的code键使该列表的深层复制或任何其他对象支持序列化:

You can use the below code to make a deep copy of the list or any other object supporting serialization:

另外,你可以用任何版本的.NET框架从2.0版及以上,以及类似的技术可以应用(除去仿制药的使用量)和使用1.1​​以及

Also you can use this for any version of .NET framework from v 2.0 and above, and the similar technique can be applied (removing the usage of generics) and used in 1.1 as well

public static class GenericCopier<T>
{
    public static T DeepCopy(object objectToCopy)
    {
        using (MemoryStream memoryStream = new MemoryStream())
        {
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            binaryFormatter.Serialize(memoryStream, objectToCopy);
            memoryStream.Seek(0, SeekOrigin.Begin);
            return (T) binaryFormatter.Deserialize(memoryStream);
        }
    }
}

您可以通过调用它

List<int> deepCopiedList = GenericCopier<List<int>>.DeepCopy(originalList);

全部code来测试,如果这个工程:

Full code to test if this works:

static void Main(string[] args)
{
    List<int> originalList = new List<int>(5);
    Random random = new Random();
    for(int i = 0; i < 5; i++)
    {
        originalList.Add(random.Next(1, 100));
        Console.WriteLine("List[{0}] = {1}", i, originalList[i]);
    }
    List<int> deepCopiedList = GenericCopier<List<int>>.DeepCopy(originalList);
    for (int i = 0; i < 5; i++)
        Console.WriteLine("deepCopiedList[{0}] value is {1}", i, deepCopiedList[i]);
}

这篇关于克隆列表&LT; T&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆