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

  • 相关阅读:
    select在各个浏览器中的兼容性问题
    pc打开手机站提示切换为手机屏幕
    图片预加载
    div盒子水平垂直居中的方法
    如何检测该浏览器为微信浏览器
    小箭头的写法,z-index在ie7显示混乱问题
    微信a标签不跳转
    css font简写规则
    windows环境cmd下执行jar
    docker制作镜像步骤
  • 原文地址:https://www.cnblogs.com/boneking/p/1333567.html
Copyright © 2011-2022 走看看