[英]C# Copy the Class List
Here is my Class: 这是我的课:
public class Information
{
public Tuple<string, string> pair { get; set; }
public double Signal { get; set; }
public double SigmaMove { get; set; }
public DateTime Date { get; set; }
}
Inforamtion A = new Information() {};
I want to use A.Clone()
but there is no such method? 我想使用
A.Clone()
但没有这样的方法?
How to Copy a List<Information> A = B
by value not by reference(Change of A did not influent B, previous solutions in the website do not work .... I tried)? 如何通过值而不是通过引用复制
List<Information> A = B
(A的更改没有影响B,网站上的先前解决方案不起作用....我尝试过)?
var TodayInformation = YesterdayInformation.Select(o => new Information { pair = o.pair, Signal = o.Signal, SigmaMove = o.SigmaMove, Date = o.Date, }).ToList();
If I copy this way, and suppose pair is also a complicate new Class
(eg List<NewClass>
), so does pair = o.pair,
need be modified to guarantee TodayInformation
is passed by value? 如果我以这种方式复制,并且假设对也是一个复杂的新
Class
(例如List<NewClass>
),那么pair = o.pair,
是否需要修改以确保TodayInformation
通过值传递?
When I add a Initializing Constructor in the Class Information
当我在类
Information
添加初始化构造函数时
public class Information { public Tuple<string, string> pair { get; set; } public double Signal { get; set; } public double SigmaMove { get; set; } public DateTime Date { get; set; } public Information(Information Tem) { pair = Tem.pair; Signal = Tem.Signal ; SigmaMove = Tem.SigmaMove; Date = Tem.Date; } }
Then Inforamtion A = new Information(){};
然后,
Inforamtion A = new Information(){};
this constructor no longer work, is it becasue of covered by the new Constructor? 此构造函数不再起作用,是否因为新的Constructor覆盖了它? Then how to remain this constructor?
那么如何保留这个构造函数呢?
You could overload the constructor, that way the empty one can still be used as well as one that takes in an object to copy eg. 您可以重载构造函数,这样仍然可以使用空的构造函数,也可以使用接收对象进行复制的构造函数。
public class Information
{
public Tuple<string, string> pair { get; set; }
public double Signal { get; set; }
public double SigmaMove { get; set; }
public DateTime Date { get; set; }
public Information()
{}
public Information(Information Tem)
{
pair = Tem.pair;
Signal = Tem.Signal ;
SigmaMove = Tem.SigmaMove;
Date = Tem.Date;
}
}
An object can be cloned implementing IClonable
interface 可以克隆实现
IClonable
接口的对象
public class Information : ICloneable { public Tuple<string, string> pair { get; set; } public double Signal { get; set; } public double SigmaMove { get; set; } public DateTime Date { get; set; } public Information(Information Tem) { pair = Tem.pair; Signal = Tem.Signal ; SigmaMove = Tem.SigmaMove; Date = Tem.Date; } #region ICloneable Members public Information Clone() { return new Information(this); } object ICloneable.Clone() { return Clone(); } #endregion }
You can use extension method as following 您可以使用扩展方法如下
static class Extensions
{
public static IList<T> Clone<T>(this IList<T> listToClone) where T: ICloneable
{
return listToClone.Select(item => (T)item.Clone()).ToList();
}
}
OR If you do not want to use IClonable interface 或如果您不想使用IClonable接口
var InformationList = new List<Information>(infoList);
EDIT 编辑
If there is a nested complex class, make sure you have also implemented IClonable
interface for that class too and clone it. 如果存在嵌套的复杂类,请确保您也为该类实现了
IClonable
接口并进行克隆。
You can overload a constructor (as Kirlac mentioned) 您可以重载构造函数(如Kirlac所述)
public class Information { public Tuple<string, string> pair { get; set; } public double Signal { get; set; } public double SigmaMove { get; set; } public DateTime Date { get; set; } public Information() {} public Information(Information Tem) { pair = Tem.pair; Signal = Tem.Signal ; SigmaMove = Tem.SigmaMove; Date = Tem.Date; } }
您的班级information
需要实现iClonable Go,请参阅如何克隆对象中的一些答案。
Not a very elegant method, in fact crude, but works .... I serialize the list into a json string and deserialize it back into the new required list. 这不是一个非常优雅的方法,实际上是粗略的,但是可以工作....我将列表序列化为json字符串,然后反序列化为新的所需列表。 The reference between the two is no longer there and each list can be manipulated independently of the other.
两者之间的引用不再存在,并且每个列表都可以独立于彼此进行操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.