序列化


        
public   static   byte [] ToBinary(AppCommand item)
        
{
            BinaryFormatter format 
= new BinaryFormatter();
            MemoryStream str 
= new MemoryStream();
            format.Serialize(str, item);
            
return str.ToArray();
        }


        
public   static  AppCommand FromBinary( byte [] data)
        
{
            BinaryFormatter format 
= new BinaryFormatter();
            MemoryStream str 
= new MemoryStream(data);
            str.Position 
= 0;
            
return (AppCommand)format.Deserialize(str);
        }

public   static   object  Clone( object  obj)
    
{
      
using (MemoryStream buffer = new MemoryStream())
      
{
        ISerializationFormatter formatter 
=
          SerializationFormatterFactory.GetFormatter();
        formatter.Serialize(buffer, obj);
        buffer.Position 
= 0;
        
object temp = formatter.Deserialize(buffer);
        
return temp;
      }

    }
 
实现压缩ViewState的Util类
//FromBase64String 将指定的 String(它将二进制数据编码为 base 64 数字)转换成等效的 8 位无符号整数数组
/// <summary>
/// GZipCompress 的摘要说明
/// </summary>

public   class  GZipCompress
{
    
//序列化工具,LosFormatter是页面默认的序列器
    private static LosFormatter _formatter = new LosFormatter();
    
/// <summary>
    
/// 解压并反序列化状态内容
    
/// </summary>
    
/// <param name="stateString">从客户端取回的页面状态字符串</param>
    
/// <returns>还原后的页面状态Pair对象</returns>

    public static object Decompress(string stateString)
    
{
        
//先把取回的状态字符串转回压缩后的数组
        byte[] buffer = Convert.FromBase64String(stateString);
        
//解压
        MemoryStream ms = new MemoryStream(buffer);
        GZipStream zipStream 
= new GZipStream(ms, CompressionMode.Decompress);
        MemoryStream msReader 
= new MemoryStream();
        buffer 
= new byte[0x1000];
        
while (true)
        
{
            
int read = zipStream.Read(buffer, 0, buffer.Length);
            
if (read <= 0)
            
{
                
break;
            }

            msReader.Write(buffer, 
0, read);
        }

        zipStream.Close();
        ms.Close();
        msReader.Position 
= 0;
        buffer 
= msReader.ToArray();
        stateString 
= Convert.ToBase64String(buffer);
        
//反序列化
        return _formatter.Deserialize(stateString);
    }

    
/// <summary>
    
/// 序列化并压缩状态内容
    
/// </summary>
    
/// <param name="state">页面状态</param>
    
/// <returns>结果字符串</returns>

    public static string Compress(object state)
    
{
        StringWriter writer 
= new StringWriter();
        
//序列化状态
        _formatter.Serialize(writer, state);
        
//取得序列化结果
        string stateString = writer.ToString();
        writer.Close();        
        
//压缩序列化状态
        byte[] buffer = Convert.FromBase64String(stateString);
        MemoryStream ms 
= new MemoryStream();
        GZipStream zipStream 
= new GZipStream(ms, CompressionMode.Compress, true);
        zipStream.Write(buffer, 
0, buffer.Length);
        zipStream.Close();
        buffer 
= new byte[ms.Length];
        ms.Position 
= 0;
        ms.Read(buffer, 
0, buffer.Length);
        ms.Close();
        
//将压缩结果转成Base64字符串,以便存到页面中
        stateString = Convert.ToBase64String(buffer);
        
return stateString;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值