Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在没有参考C的情况下复制列表_C#_Asp.net_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

C# 如何在没有参考C的情况下复制列表

C# 如何在没有参考C的情况下复制列表,c#,asp.net,asp.net-mvc,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,我有这个错误 System.Runtime.Serialization.SerializationException:'类型 程序集“Testcloneobject”中的“Testcloneobject.Models.MatchTeam”, 版本=1.0.0.0,区域性=中性,未标记PublicKeyToken=null 可序列化 您在图像中发布的错误请以文本而不是图像的形式发布,与示例中的类不匹配。但是,请再次阅读错误消息。它确实准确地说明了代码中缺少的内容。简单的LINQ.Select可以“

我有这个错误


System.Runtime.Serialization.SerializationException:'类型 程序集“Testcloneobject”中的“Testcloneobject.Models.MatchTeam”, 版本=1.0.0.0,区域性=中性,未标记PublicKeyToken=null 可序列化


您在图像中发布的错误请以文本而不是图像的形式发布,与示例中的类不匹配。但是,请再次阅读错误消息。它确实准确地说明了代码中缺少的内容。简单的LINQ.Select可以“非通用”地用于创建具有相同数据的新对象。如果由于其他原因不需要这个序列化,我可能完全删除它。也许,考虑每个匹配组都有一个易于添加的克隆方法。这会如何改变方法?System.Runtime.Serialization.SerializationException:“程序集”Testcloneobject,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null“中的类型”Testcloneobject.Models.MatchTeam“未标记为可序列化。我有一个错误@GvSYou将MatchTeamModel标记为[Serializable]。您会得到一个错误,MatchTeam不可序列化。由于MatchTeam由MatchTeamModel引用,因此需要根据需要对实例进行序列化和反序列化。
using System.Runtime.Serialization.Formatters.Binary;

public class MatchTeam
   {
       public int StudentID { get; set; }
       public int PositionName { get; set; }
       public int GroupTeamNumber { get; set; }
   }

[Serializable]

   public class MatchTeamModel : ICloneable
   {
       public List<MatchTeam> MatchAllStudentList { get; set; }

       public object Clone()
       {
           using (MemoryStream stream = new MemoryStream())
           {
               if (this.GetType().IsSerializable)
               {
                   BinaryFormatter formatter = new BinaryFormatter();
                   formatter.Serialize(stream, this);
                   stream.Position = 0;
                   return formatter.Deserialize(stream);
               }
               return null;
           }
       }
   }


 MatchTeamModel Firstlist= new MatchTeamModel
            {
                MatchAllStudentList = new List<MatchTeam>()
            };

 MatchTeamModel Secondlist = new MatchTeamModel
            {
                MatchAllStudentList = new List<MatchTeam>()
            };

MatchTeam MT1 = new MatchTeam { StudentID = 1,Positionname = center,groupteamnumber = 1};
MatchTeam MT2 = new MatchTeam { StudentID = 2 ,Positionname = Forward ,groupteamnumber = 1};
MatchTeam MT3 = new MatchTeam { StudentID = 3,Positionname = Guard,groupteamnumber = 1};
MatchTeam MT4 = new MatchTeam { StudentID = 4,Positionname = center,groupteamnumber = 2};
MatchTeam MT5 = new MatchTeam { StudentID = 5,Positionname = Forward,groupteamnumber = 2};
MatchTeam MT6 = new MatchTeam { StudentID = 6,Positionname = Guard,groupteamnumber = 2};

Firstlist.MatchAllStudentList.Add(MT1)
Firstlist.MatchAllStudentList.Add(MT2)
Firstlist.MatchAllStudentList.Add(MT3)
Firstlist.MatchAllStudentList.Add(MT4)
Firstlist.MatchAllStudentList.Add(MT5)
Firstlist.MatchAllStudentList.Add(MT6)



Secondlist = Firstlist;
Secondlist = Firstlist.Clone() as MatchTeamModel;