• 序列化


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Serialization;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;

    namespace WordDocumentSocket
    {
        /// <summary>
        /// 序列化
        /// </summary>
        public class Serializer
        {
            /// <summary>
            /// 序列化
            /// </summary>
            /// <param name="objectToConvert"></param>
            /// <param name="path"></param>
            /// <param name="encoding"></param>
            public void PositiveSerializer(object objectToConvert, string path, Encoding encoding)
            {
                // 对象不为空
                if (objectToConvert != null)
                {
                    Type t = objectToConvert.GetType();
                    XmlSerializer ser = new XmlSerializer(t);
                    using (StreamWriter writer = new StreamWriter(path, false, encoding))
                    {
                        ser.Serialize(writer, objectToConvert);
                        writer.Close();
                    }
                }
            }

            /// <summary>
            /// 反序列化
            /// </summary>
            /// <param name="path">路经加文件名</param>
            /// <param name="objectType">内容类型</param>
            /// <param name="encoding">编码类型</param>
            /// <returns></returns>
            public object InsteadSerializer(string path, Type objectType, Encoding encoding)
            {
                object convertedObject = null;
                // 文件名不为空
                if (!string.IsNullOrEmpty(path))
                {
                    XmlSerializer ser = new XmlSerializer(objectType);
                    using (StreamReader reader = new StreamReader(path, encoding))
                    {
                        convertedObject = ser.Deserialize(reader);
                        reader.Close();
                    }
                }
                return convertedObject;
            }
        }
    }

    [Serializable]
        public class UserInfo
        {
            private string ifOpen;

            public string IfOpen
            {
                get { return ifOpen; }
                set { ifOpen = value; }
            }
            private string addressIp;

            public string AddressIp
            {
                get { return addressIp; }
                set { addressIp = value; }
            }
            private int port;

            public int Port
            {
                get { return port; }
                set { port = value; }
            }
        }

    UserInfo userinfo = new UserInfo();
                string path = @"xsm.xml";
                userinfo.IfOpen = "1";
                userinfo.AddressIp = GetIPAddress();
                userinfo.Port = 1027;
                sl.PositiveSerializer(userinfo, path, Encoding.UTF8);
                object o = sl.InsteadSerializer(path, typeof(UserInfo), Encoding.UTF8);
                UserInfo d = o as UserInfo;
                this.Paragraphs[1].Range.Text = "xsmhero "+d.IfOpen+" "+d.AddressIp +"  "+d.Port;

  • 相关阅读:
    【洛谷P2660烤鸡】
    cogs448
    排队打水
    洛谷U36590搬书
    NOIP2012借教室
    归并排序模版
    NOIP2015神奇的幻方
    NOIP2006能量项链
    NOIP2003加分二叉树
    NOI1995石子合并&多种石子合并
  • 原文地址:https://www.cnblogs.com/xsmhero/p/1436891.html
Copyright © 2020-2023  润新知