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));
            }

  • 相关阅读:
    0621回顾与总结
    0614--演示
    学习进度条
    实验四主存空间的分配和回收
    学术诚信和职业道德
    《构建之法》读第六、第七章有感
    0422 Step2-FCFS调度
    java 版的复利计算器(张俊毅 周修文)
    复利程序(c语言)(张俊毅 周修文)
    《构建之法》第4章
  • 原文地址:https://www.cnblogs.com/boneking/p/1333567.html
Copyright © 2011-2022 走看看