• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

C#自定义序列化 ISerializable 的实现

OC/C/C++ 水墨上仙 1596次浏览

C#自定义序列化 ISerializable 的实现
转自:http://blog.csdn.net/heavensdoor/article/details/6325169

   [Serializable]
    public class BaseObject
    {
        [OptionalField]
        private string _str = "I am BaseObject";
    }
    [Serializable]
    public class TestObject : BaseObject, ISerializable
    {
        int a;
        string strName = "";
        Color c = Color.Red;
        DataTable _dtColors = null;
        [OptionalField]
        ArrayList list = new ArrayList();
        [OptionalField]
        List<int> list1 = new List<int>();
        [OptionalField]
        Dictionary<int, string> dic = new Dictionary<int, string>();
        //当实现ISerializable接口时,如果该构造函数不存在,则会引发一个SerializationException异常
        //该特性表示,该方法只允许序列化器调
        [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
        protected TestObject(SerializationInfo info, StreamingContext context)
        {
            #region  如果基类也实现了ISerializable接口,则序列化器会自动调用基类的该构造函数,就不需要本段代码
            Type basetype = this.GetType().BaseType;
            MemberInfo[] mi = FormatterServices.GetSerializableMembers(basetype, context);
            for (int i = 0; i < mi.Length; i++)
            {
                //由于AddValue不能添加重名值,为了避免子类变量名与基类变量名相同,将基类序列化的变量名加上基类类名
                FieldInfo fi = (FieldInfo)mi[0];
                object objValue = info.GetValue(basetype.FullName + "+" + fi.Name, fi.FieldType);
                fi.SetValue(this, objValue);
            }
            #endregion
            a = info.GetInt32("a");
            strName = info.GetString("strName");
            c = (Color)info.GetValue("c", typeof(Color));
            _dtColors = (DataTable)info.GetValue("_dtColors", typeof(DataTable));
            list = (ArrayList)info.GetValue("list", typeof(ArrayList));
            list1 = (List<int>)info.GetValue("list1", typeof(List<int>));
            dic = (Dictionary<int, string>)info.GetValue("dic", typeof(Dictionary<int, string>));
        }
        public TestObject()
        {
            a = 100;
            strName = "daps";
            InitColorTable();
            list1.Add(10);
           list1.Add(20);       
        }
        #region ISerializable 成员
        [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter =true)]
        void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("a", a);
            info.AddValue("strName", strName);
            info.AddValue("c", c);
            info.AddValue("_dtColors", _dtColors);
            info.AddValue("list", list);
            info.AddValue("list1", list1);
            info.AddValue("dic", dic);
            Type basetype = this.GetType().BaseType;
            MemberInfo[] mi = FormatterServices.GetSerializableMembers(basetype, context);
            for (int i = 0; i < mi.Length; i++)
            {
                //由于AddValue不能添加重名值,为了避免子类变量名与基类变量名相同,将基类序列化的变量名加上基类类名
                info.AddValue(basetype.FullName + "+" + mi[i].Name, ((FieldInfo)mi[i]).GetValue(this));
            }
        }
        #endregion
    }


喜欢 (0)
加载中……