Exception

SqlClient

是不可以系列化的

 

 

        public static string BinarySerialize(object o)
        {
            string result = null;
            if (o != null)
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    BinaryFormatter formatter = new BinaryFormatter();
                    formatter.Serialize(stream, o);
                    stream.Position = 0;
                    result = Convert.ToBase64String(stream.ToArray());
                }
            }
            return result;
        }

        public static T BinaryDeserialize<T>(string hash)
        {
            T result = default(T);
            if (!string.IsNullOrEmpty(hash))
            {
                using (MemoryStream stream = new MemoryStream(Convert.FromBase64String(hash)))
                {
                    stream.Position = 0;
                    BinaryFormatter formatter = new BinaryFormatter();
                    result = (T)formatter.Deserialize(stream);
                }
            }
            return result;
        }

posted on 2009-04-20 20:26  Sky Wang  阅读(220)  评论(0编辑  收藏  举报