zoukankan      html  css  js  c++  java
  • [C#]序列化例子

     

        [Serializable]
        public class SolidButton : Button, ISerializable
        {
            public SolidButton(SerializationInfo info, StreamingContext ctxt)
            {
                this.Location = new Point((int)info.GetValue("X", typeof(int)), (int)info.GetValue("Y", typeof(int)));
                this.Text = (String)info.GetValue("Text", typeof(string));
                this.Height = (int)info.GetValue("Height", typeof(int));
                this.Width = (int)info.GetValue("Width", typeof(int));
                this.Name = (String)info.GetValue("Name", typeof(string));
            }

            public SolidButton()
            {
            }

            public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
            {
                info.AddValue("Text", this.Text);
                info.AddValue("X", this.Location.X);
                info.AddValue("Y", this.Location.Y);
                info.AddValue("Height", this.Height);
                info.AddValue("Width", this.Width);
                info.AddValue("Name", this.Name);
            }
        }

     

     

     

     

           /// <summary>
            /// 把对象序列化并返回相应的字节
            /// </summary>
            /// <param name="pObj">需要序列化的对象</param>
            /// <returns>byte[]</returns>
            public byte[] ObjectToByte(object pObj)
            {
                if (pObj == null)
                {
                    return null;
                }
                System.IO.MemoryStream _memory = new System.IO.MemoryStream();
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(_memory, pObj);
                _memory.Position = 0;
                byte[] read = new byte[_memory.Length];
                _memory.Read(read, 0, read.Length);
                _memory.Close();
                return read;
            }

            /// <summary>
            /// 把对象序列化并返回相应的字符串
            /// </summary>
            /// <param name="pObj">需要序列化的对象</param>
            /// <returns></returns>
            public string ObjectToString(object pObj)
            {
                return ByteToString(ObjectToByte(pObj));
            }

            /// <summary>
            /// 把字节反序列化成相应的对象
            /// </summary>
            /// <param name="pBytes">字节流</param>
            /// <returns>object</returns>
            public object ByteToObject(byte[] pBytes)
            {
                object _newOjb = null;
                if (pBytes == null)
                {
                    return _newOjb;
                }
                System.IO.MemoryStream _memory = new System.IO.MemoryStream(pBytes);
                _memory.Position = 0;
                BinaryFormatter formatter = new BinaryFormatter();
                _newOjb = formatter.Deserialize(_memory);
                _memory.Close();
                return _newOjb;
            }

            /// <summary>
            /// 把字符串反序列化成相应的对象
            /// </summary>
            /// <param name="pStr"></param>
            /// <returns></returns>
            public object StringToObject(string pStr)
            {
                return ByteToObject(StringToByte(pStr));
            }

  • 相关阅读:
    spoj 694 求一个字符串中不同子串的个数
    Qt for Android 开发大坑
    HDUOJ A Mathematical Curiosity 1017
    Node.js开发入门—HelloWorld再分析
    GTK入门学习:布局容器之固定布局
    彻底领悟javascript中的exec与match方法
    JQuery中attr属性和jQuery.data()学习笔记
    正则表达式-验证带千分号的,带任意位小数的数字类型
    JQuery EasyUI 动态改变表单项的验证守则
    JavaScript计算两个日期的时间差
  • 原文地址:https://www.cnblogs.com/boneking/p/1333567.html
Copyright © 2011-2022 走看看