序列化

        }

        #endregion

 

        #region SoapFormatter序列化

        /// <summary>

        /// SoapFormatter序列化

        /// 必须类型必须标记为Serializable

        /// </summary>

        /// <param name="obj"></param>

        /// <returns></returns>

        public static string SerializeSoapFormatter(object obj)

        {

            SoapFormatter formatter = new SoapFormatter();

            using (MemoryStream ms = new MemoryStream())

            {

                formatter.Serialize(ms, obj);

                byte[] bytes = ms.ToArray();

                return Encoding.UTF8.GetString(bytes);

            }

        }

        /// <summary>

        /// SoapFormatter反序列化

        /// 必须类型必须标记为Serializable

        /// </summary>

        /// <param name="serializedStr"></param>

        /// <returns></returns>

        public static T DeserializeSoapFormatter<T>(string serializedStr)

        {

            SoapFormatter formatter = new SoapFormatter();

            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(serializedStr)))

            {

                return (T)formatter.Deserialize(ms);

            }

        }

        #endregion

原文地址:https://www.cnblogs.com/lyl6796910/p/3803052.html