/// <summary>
        /// 序列化
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        private byte[] Serialize(object obj)
        {
            if (obj == null)
            {
                return(null);
            }

            var binaryFormatter = new BinaryFormatter.BinaryConverter();

            using (var memoryStream = new MemoryStream())
            {
                binaryFormatter.Serialize(obj, memoryStream);
                var data = memoryStream.ToArray();
                return(data);
            }
        }
        //public static T Get<T>(string key)
        //{
        //    var connect = AzureredisDb.Cache;
        //    var r = AzureredisDb.Cache.StringGet(key);
        //    return Deserialize<T>(r);
        //}

        //public static List<T> GetList<T>(string key)
        //{
        //    return (List<T>)Get(key);
        //}

        //public static void SetList<T>(string key, List<T> list)
        //{
        //    Set(key, list);
        //}

        //public static object Get(string key)
        //{
        //    return Deserialize<object>(AzureredisDb.Cache.StringGet(key));
        //}

        //public static void Set(string key, object value)
        //{
        //    AzureredisDb.Cache.StringSet(key, Serialize(value));
        //}

        /// <summary>
        /// 序列化对象
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static byte[] Serialize(this object o)
        {
            if (o == null)
            {
                return(null);
            }

            var dtx = SystemTime.Now;

#if NETSTANDARD2_0 || (NETSTANDARD2_1 || NETCOREAPP3_0)
            ////二进制序列化方案
            //using (MemoryStream memoryStream = new MemoryStream())
            //{

            //    ProtoBuf.Serializer.Serialize(memoryStream, o);
            //    byte[] objectDataAsStream = memoryStream.ToArray();
            //    return objectDataAsStream;
            //}

            BinaryFormatter.BinaryConverter binaryConverter = new BinaryFormatter.BinaryConverter();
            return(binaryConverter.Serialize(o));
#else
            #region .net 4.5 和 .net core 2.0 都提供对 BinaryFormatter 的支持,但是 .net core 2.0 不支持委托的序列化
            //二进制序列化方案
            var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            using (MemoryStream memoryStream = new MemoryStream())
            {
                binaryFormatter.Serialize(memoryStream, o);
                byte[] objectDataAsStream = memoryStream.ToArray();
                return(objectDataAsStream);
            }
            #endregion
#endif

            //Console.WriteLine($"StackExchangeRedisExtensions.Serialize耗时:{SystemTime.DiffTotalMS(dtx)}ms");


            //使用JSON序列化,会在Get()方法反序列化到IContainerBag的过程中出错
            //JSON序列化方案
            //SerializerHelper serializerHelper = new SerializerHelper();
            //var jsonSetting = serializerHelper.GetJsonString(o);
            //return Encoding.UTF8.GetBytes(jsonSetting);
        }
Пример #3
0
        //public static T Get<T>(string key)
        //{
        //    var connect = AzureredisDb.Cache;
        //    var r = AzureredisDb.Cache.StringGet(key);
        //    return Deserialize<T>(r);
        //}

        //public static List<T> GetList<T>(string key)
        //{
        //    return (List<T>)Get(key);
        //}

        //public static void SetList<T>(string key, List<T> list)
        //{
        //    Set(key, list);
        //}

        //public static object Get(string key)
        //{
        //    return Deserialize<object>(AzureredisDb.Cache.StringGet(key));
        //}

        //public static void Set(string key, object value)
        //{
        //    AzureredisDb.Cache.StringSet(key, Serialize(value));
        //}

        /// <summary>
        /// 序列化对象
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static byte[] Serialize(this object o)
        {
            if (o == null)
            {
                return(null);
            }

#if NETSTANDARD2_0
            ////二进制序列化方案
            //using (MemoryStream memoryStream = new MemoryStream())
            //{

            //    ProtoBuf.Serializer.Serialize(memoryStream, o);
            //    byte[] objectDataAsStream = memoryStream.ToArray();
            //    return objectDataAsStream;
            //}

            BinaryFormatter.BinaryConverter binaryConverter = new BinaryFormatter.BinaryConverter();
            return(binaryConverter.Serialize(o));
#else
            #region .net 4.5 和 .net core 2.0 都提供对 BinaryFormatter 的支持,但是 .net core 2.0 不支持委托的序列化
            //二进制序列化方案
            var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            using (MemoryStream memoryStream = new MemoryStream())
            {
                binaryFormatter.Serialize(memoryStream, o);
                byte[] objectDataAsStream = memoryStream.ToArray();
                return(objectDataAsStream);
            }
            #endregion
#endif

            //使用JSON序列化,会在Get()方法反序列化到IContainerBag的过程中出错
            //JSON序列化方案
            //SerializerHelper serializerHelper = new SerializerHelper();
            //var jsonSetting = serializerHelper.GetJsonString(o);
            //return Encoding.UTF8.GetBytes(jsonSetting);
        }