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


 
using System;
using System.Collections.Generic;
using System.Text;

using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Common
{
    [Serializable]
    public class GroupNameXML
    {
        // 名称
        private string m_Name;

        /// <summary>
        /// 名称
        /// </summary>
        public string name
        {
            get { return m_Name; }
            set { m_Name = value; }
        }
    }

    public class GroupNameXmlList : List<GroupNameXML>, ICloneable 
    { 
        public object Clone()
        {
            MemoryStream ms = new MemoryStream();
            object obj;
            try
            {
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(ms, this);
                ms.Seek(0, SeekOrigin.Begin);
                obj = bf.Deserialize(ms);
            }
            finally
            {
                ms.Close();
            }

            return obj;
        }
    }
}
使用:

GroupNameXmlList groupNameXmlList = new GroupNameXmlList();

GroupNameXML groupNameXML = new GroupNameXML();
groupNameXML.name = "haha";
groupNameXmlList.Add(groupNameXML);
//以上三句可循环操作

GroupNameXmlList groupTempList = (GroupNameXmlList)groupNameXmlList.Clone();

posted on 2010-02-03 15:57  RIVERSPIRIT  阅读(2176)  评论(0)    收藏  举报