加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

二进制泛型序列化.

发布时间:2020-12-14 04:36:49 所属栏目:大数据 来源:网络整理
导读:public static class BinarySerializationHelper{ public static void ToBinaryFileT( this T tempSerializeList, string path) // 二进制序列化 { // 路径 var fs = new FileStream(path,FileMode.Create); try { BinaryFormatter bf = new BinaryFormatter
public static class BinarySerializationHelper
{

    public static void ToBinaryFile<T>(this T tempSerializeList,string path)   // 二进制序列化  
    {
        //路径

        var fs = new FileStream(path,FileMode.Create);
        try
        {
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(fs,tempSerializeList);
            fs.Close();
        }
        catch (Exception ex)
        {
        }
        finally
        {
            fs.Close();
        }
    }

    public static T FromBinaryFile<T>(string path)   // 二进制反序列化  
    {
        T tempDeserializeList;
        FileInfo binaryFile = new FileInfo(path);
        if (!binaryFile.Exists)
        {
            return default(T);
        }
        FileStream fs = new FileStream(path,FileMode.Open);
        try
        {
            BinaryFormatter bf = new BinaryFormatter();

            tempDeserializeList = (T)bf.Deserialize(fs);
            return tempDeserializeList;
        }
        catch (Exception ex)
        {
            File.Delete(path);
            return default(T);
        }
        finally
        {
            fs.Close();
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读