现在的位置: 首页 > 综合 > 正文

C#自定义集合List及复制操作

2013年12月09日 ⁄ 综合 ⁄ 共 1197字 ⁄ 字号 评论关闭
 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.Runtime.Serialization.Formatters.Binary;
  6. namespace Common
  7. {
  8.     [Serializable]
  9.     public class GroupNameXML
  10.     {
  11.         // 名称
  12.         private string m_Name;
  13.         /// <summary>
  14.         /// 名称
  15.         /// </summary>
  16.         public string name
  17.         {
  18.             get { return m_Name; }
  19.             set { m_Name = value; }
  20.         }
  21.     }
  22.     public class GroupNameXmlList : List<GroupNameXML>, ICloneable 
  23.     { 
  24.         public object Clone()
  25.         {
  26.             MemoryStream ms = new MemoryStream();
  27.             object obj;
  28.             try
  29.             {
  30.                 BinaryFormatter bf = new BinaryFormatter();
  31.                 bf.Serialize(ms, this);
  32.                 ms.Seek(0, SeekOrigin.Begin);
  33.                 obj = bf.Deserialize(ms);
  34.             }
  35.             finally
  36.             {
  37.                 ms.Close();
  38.             }
  39.             return obj;
  40.         }
  41.     }
  42. }

使用:

  1. GroupNameXmlList groupNameXmlList = new GroupNameXmlList();
  2. GroupNameXML groupNameXML = new GroupNameXML();
  3. groupNameXML.name = "haha";
  4. groupNameXmlList.Add(groupNameXML);
  5. //以上三句可循环操作
  6. GroupNameXmlList groupTempList = (GroupNameXmlList)groupNameXmlList.Clone();

抱歉!评论已关闭.