简体   繁体   English

C#复制课程表

[英]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() {};
  1. I want to use A.Clone() but there is no such method? 我想使用A.Clone()但没有这样的方法?

  2. 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通过值传递?

  3. 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;
    }
}
  1. 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 } 
  2. You can copy a list by value. 您可以按值复制列表。 Follow any of the steps below If you want to know more about extension method follow this link 请执行以下任何步骤如果您想了解更多有关扩展方法的信息,请点击此链接

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接口并进行克隆。

  1. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM