我有这门课:
public class Person : ICloneable
{
public string FirstName { get; set; }
public string LastName { get; set; }
public object Clone()
{
return this;
}
}
扩展方法:
public static class MyHelper
{
public static IEnumerable<T> Clone<T>(this IEnumerable<T> collection) where T : ICloneable
{
return collection.Select(item => (T)item.Clone());
}
}
我想在这种情况下使用它:
var myList = new List<Person>{
new Person { FirstName = "Dana", LastName = "Scully" },
new Person{ FirstName = "Fox", LastName = "Mulder" }
};
List<Person> myCopy = myList.Clone().ToList<Person>();
当我在“即时窗口”中更改 的值时myCopy
,原始列表也发生了变化。
我希望两个列表都完全独立
我错过了什么?