• 序列化,反序列化方法



            /// <summary>
            /// 序列化对象
            /// </summary>
            /// <param name="obj"></param>
            /// <returns></returns>
            public static string SerializeObject(Object obj)
            {
                IFormatter ifor = new BinaryFormatter();
                string str = "";
                using (MemoryStream sm = new MemoryStream())
                {
                    ifor.Serialize(sm, obj);                        //序列化
                    sm.Seek(0, SeekOrigin.Begin);

                    byte[] bytes = new byte[sm.Length];
                    bytes = sm.ToArray();
                    str = Convert.ToBase64String(bytes);
                    str = HttpContext.Current.Server.UrlEncode(str);//编码
                }
                return str;
            }

            #endregion

            #region 反序列化字符串为对象
            /// <summary>
            /// 反序列化对象
            /// </summary>
            /// <returns></returns>
            public static object DeserializeObject(string str)
            {
                str = HttpContext.Current.Server.UrlDecode(str);    //解码
                byte[] bt = Convert.FromBase64String(str);
                Stream smNew = new MemoryStream(bt);
                IFormatter fmNew = new BinaryFormatter();
                object obj = fmNew.Deserialize(smNew);              //反序列化
                return obj;
            }

            #endregion


            /**//// <summary>
            /// 对象序列化,得到Base64字符串
            /// </summary>
            /// <param name="Obj">待序列化的对象</param>
            /// <returns></returns>
            public static string ObjectToBase64Str(Object Obj)
            {
                return Convert.ToBase64String(ObjToBytes(Obj));
            }

            /**//// <summary>
            /// Base64字符串反序列化,得到对象
            /// </summary>
            /// <param name="ScrStr">待反序列化的字符串</param>
            /// <param name="ObjType">对象类型</param>
            /// <returns></returns>
            public static Object Base64StrToObject(string ScrStr, Type ObjType)
            {
                return BytesToObj(Convert.FromBase64String(ScrStr), ObjType);
            }

            /**//// <summary>
            /// 对象序列化,得到字符串
            /// </summary>
            /// <param name="Obj">待序列化的对象</param>
            /// <returns></returns>
            public static string ObjectToString(Object Obj)
            {
                return Encoding.Default.GetString(ObjToBytes(Obj));
            }

            /**//// <summary>
            /// 字符串反序列化,得到对象
            /// </summary>
            /// <param name="ScrStr">待反序列化的字符串</param>
            /// <param name="ObjType">对象类型</param>
            /// <returns></returns>
            public static Object StringToObject(string ScrStr, Type ObjType)
           {
                return BytesToObj(Encoding.Default.GetBytes(ScrStr), ObjType);
            }
           
            /**//// <summary>
            /// 将对象序列化为字节数组
            /// </summary>
            /// <param name="Obj"></param>
            /// <returns></returns>
            private static byte[] ObjToBytes(Object Obj)
            {
                XmlSerializer ser = new XmlSerializer(Obj.GetType());
                MemoryStream mem = new MemoryStream();
                XmlTextWriter writer = new XmlTextWriter(mem, Encoding.Default);
                ser.Serialize(writer, Obj);
                writer.Close();

                return mem.ToArray();
            }

            /**//// <summary>
            /// 从字节数组反序列化得到对象
            /// </summary>
            /// <param name="ObjBytes"></param>
            /// <returns></returns>
            private static Object BytesToObj(byte[] ObjBytes, Type ObjType)
            {
                try
                {
                    XmlSerializer ser = new XmlSerializer(ObjType);
                    MemoryStream mem = new MemoryStream(ObjBytes);
                    StreamReader sr = new StreamReader(mem, Encoding.Default);
                    return ser.Deserialize(sr);
                }
                catch
                {
                    return null;
                }
            }

  • 相关阅读:
    Quartz.net官方开发指南 第十一课: 高级(企业级)属性
    .NET开源项目:Tomboy
    开源的作业调度框架 Quartz.NET
    DinnerNow.net: 微软最新技术集成示例
    将Quartz.NET集成到 Castle中
    评论:微软的SOA战略
    行为驱动开发Behaviour Driven Development
    Mono 1.2.5 发布,提供对IronPython和DLR的支持
    Quartz.net官方开发指南 第二课:Jobs And Triggers
    数据库厂商提供的 Providers for ASP.NET
  • 原文地址:https://www.cnblogs.com/di305449473/p/1193287.html
Copyright © 2020-2023  润新知