问题
In the following piece of code,
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace clone_test_01
{
public partial class MainForm : Form
{
public class Book
{
public string title = \"\";
public Book(string title)
{
this.title = title;
}
}
public MainForm()
{
InitializeComponent();
List<Book> books_1 = new List<Book>();
books_1.Add( new Book(\"One\") );
books_1.Add( new Book(\"Two\") );
books_1.Add( new Book(\"Three\") );
books_1.Add( new Book(\"Four\") );
List<Book> books_2 = new List<Book>(books_1);
books_2[0].title = \"Five\";
books_2[1].title = \"Six\";
textBox1.Text = books_1[0].title;
textBox2.Text = books_1[1].title;
}
}
}
I use a Book object type to create a List<T> and I populate it with a few items giving them a unique title (from \'one\' to \'five\').
Then I create List<Book> books_2 = new List<Book>(books_1).
From this point, I know it\'s a clone of the list object, BUT the book objects from book_2 are still a reference from the book objects in books_1. It\'s proven by making changes on the two first elements of books_2, and then checking those same elements of book_1 in a TextBox.
books_1[0].title and books_2[1].title have indeed been changed to the new values of books_2[0].title and books_2[1].title.
NOW THE QUESTION
How do we create a new hard copy of a List<T>? The idea is that books_1 and books_2 become completely independent of each other.
I\'m disappointed Microsoft didn\'t offer a neat, fast and easy solution like Ruby are doing with the clone() method.
What would be really awesome from helpers is to use my code and alter it with a workable solution so it can be compiled and work. I think it will truly help newbies trying to understand offered solutions for this issue.
EDIT: Note that the Book class could be more complex and have more properties. I tried to keep things simple.
回答1:
You need to create new Book objects then put those in a new List:
List<Book> books_2 = books_1.Select(book => new Book(book.title)).ToList();
Update: Slightly simpler... List<T> has a method called ConvertAll that returns a new list:
List<Book> books_2 = books_1.ConvertAll(book => new Book(book.title));
回答2:
Create a generic ICloneable<T> interface which you implement in your Book class so that the class knows how to create a copy of itself.
public interface ICloneable<T>
{
T Clone();
}
public class Book : ICloneable<Book>
{
public Book Clone()
{
return new Book { /* set properties */ };
}
}
You can then use either the linq or ConvertAll methods that Mark mentioned.
List<Book> books_2 = books_1.Select(book => book.Clone()).ToList();
or
List<Book> books_2 = books_1.ConvertAll(book => book.Clone());
回答3:
I'm disappointed Microsoft didn't offer a neat, fast and easy solution like Ruby are doing with the
clone()method.
Except that does not create a deep copy, it creates a shallow copy.
With deep copying, you have to be always careful, what exactly do you want to copy. Some examples of possible issues are:
- Cycle in the object graph. For example,
Bookhas anAuthorandAuthorhas a list of hisBooks. - Reference to some external object. For example, an object could contain open
Streamthat writes to a file. - Events. If an object contains an event, pretty much anyone could be subscribed to it. This can get especially problematic if the subscriber is something like a GUI
Window.
Now, there are basically two ways how to clone something:
- Implement a
Clone()method in each class that you need cloned. (There is alsoICloneableinterface, but you should not use that; using a customICloneable<T>interface as Trevor suggested is okay.) If you know that all you need is to create a shallow copy of each field of this class, you could use MemberwiseClone() to implement it. As an alternative, you could create a “copy constructor”:public Book(Book original). - Use serialization to serialize your objects into a
MemoryStreamand then deserialize them back. This requires you to mark each class as[Serializable]and it can also be configured what exactly (and how) should be serialized. But this is more of a “quick and dirty” solution, and will most likely also be less performant.
回答4:
List<Book> books_2 = new List<Book>(books_2.ToArray());
That should do exactly what you want. Demonstrated here.
回答5:
Well,
If you mark all involved classes as serializable you can :
public static List<T> CloneList<T>(List<T> oldList)
{
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
formatter.Serialize(stream, oldList);
stream.Position = 0;
return (List<T>)formatter.Deserialize(stream);
}
Source:
https://social.msdn.microsoft.com/Forums/en-US/5c9b4c31-850d-41c4-8ea3-fae734b348c4/copy-listsomeobject-to-clone-list?forum=csharpgeneral
回答6:
Since Clone would return an object instance of Book, that object would first need to be cast to a Book before you can call ToList on it. The example above needs to be written as:
List<Book> books_2 = books_1.Select(book => (Book)book.Clone()).ToList();
回答7:
If the Array class meets your needs, you could also use the List.ToArray method, which copies elements to a new array.
Reference: http://msdn.microsoft.com/en-us/library/x303t819(v=vs.110).aspx
回答8:
public static class Cloner
{
public static T Clone<T>(this T item)
{
FieldInfo[] fis = item.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
object tempMyClass = Activator.CreateInstance(item.GetType());
foreach (FieldInfo fi in fis)
{
if (fi.FieldType.Namespace != item.GetType().Namespace)
fi.SetValue(tempMyClass, fi.GetValue(item));
else
{
object obj = fi.GetValue(item);
if (obj != null)
fi.SetValue(tempMyClass, obj.Clone());
}
}
return (T)tempMyClass;
}
}
回答9:
You can use this:
var newList= JsonConvert.DeserializeObject<List<Book>>(list.toJson());
来源:https://stackoverflow.com/questions/14007405/how-create-a-new-deep-copy-clone-of-a-listt