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

  • 相关阅读:
    RobotFramework 安装配置(一)
    JAVA练手--集合
    JAVA文件操作
    线性布局--LinearLayout
    android studio导入android studio工程
    通过开机广播(broadcast)通知应用
    Android studio应用导入源码错误This attribute must be localized
    android studio的jni和so
    上传漏洞总结-UPLOAD-LABS
    靶机渗透测试实战(一)——熟悉渗透测试流程的简单测试
  • 原文地址:https://www.cnblogs.com/boneking/p/1333567.html
Copyright © 2011-2022 走看看