zoukankan      html  css  js  c++  java
  • C#序列化的泛型使用

    c#泛型封装序列化和反序列化方法
    1 using System;
    2  using System.Collections.Generic;
    3  using System.Linq;
    4  using System.Text;
    5 using System.IO;
    6 using System.Runtime.Serialization.Formatters.Binary;//序列化命名空间的引用
    7 public class ReadWrite
    8 {
    9 /// <summary>
    10 /// 将自定义类型反序列化
    11 /// </summary>
    12 /// <typeparam name="T">传入类型</typeparam>
    13 /// <param name="path">传入序列化文件的路径</param>
    14 /// <param name="Info">传入自定义类型变量,并带回被反序列化文件的内容</param>
    15 public bool Rbin<T>(string path, ref T Info)
    16 {
    17 try
    18 {
    19 if (File.Exists(path))
    20 {
    21 FileStream files = new FileStream(path, FileMode.Open, FileAccess.Read);//文件流
    22 BinaryFormatter open = new BinaryFormatter();//序列化类
    23 Info = (T)open.Deserialize(files);//反序列化泛型类
    24 files.Close();//关闭文件流
    25 return true;
    26 }
    27 }
    28 catch (Exception)
    29 {
    30
    31 throw;
    32 }
    33 return false;
    34 }
    35
    36
    37
    38 /// <summary>
    39 /// 将传入的 变量序列化
    40 /// </summary>
    41 /// <typeparam name="T">传入的类型</typeparam>
    42 /// <param name="bk">传入变量</param>
    43 /// <param name="path">保存路径,若文件已存在则自动重命名保存</param>
    44 /// <returns>保存是否成功ture\false</returns>
    45 public bool Wbin<T>(T bk, string path)
    46 {
    47 int name = 0; string path1 = path;
    48 try
    49 {
    50 while (File.Exists(path))//如果文件存在重命名
    51 {
    52 path = path1;
    53 int index = path.LastIndexOf('.');//找到扩展名的位置
    54 path = path.Insert(index, name.ToString());//在扩展名前加自增长数
    55 name++;
    56 }
    57 FileStream file = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);//保存路径、打开或创建文件、用于写
    58 BinaryFormatter write = new BinaryFormatter();//序列化的新类
    59 write.Serialize(file, bk);//序列的流、序列化的类对象
    60 file.Close();//关闭流
    61 return true;
    62 }
    63 catch (Exception)
    64 {
    65 throw;
    66 }
    67 return false;
    68 }
    69 }

    这两个方法可实现序列化的封装,可生成一个类库,在任何项目中引用即可。下面是做个示例,其序列化的类可是任何类

    自定义一个book类做序列化和反序列化示例
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using MySave;//引用自定义序列化类
    namespace 测试
    {
    /// <summary>
    /// 自定义的book类
    /// </summary>
    [Serializable]//可序列化的标记
    public class Book
    {

    /// <summary>
    /// 书名段
    /// </summary>
    private string _name;

    /// <summary>
    /// 书名
    /// </summary>
    public string Name
    {
    get { return _name; }
    set { _name = value; }
    }
    /// <summary>
    /// 作者
    /// </summary>
    private string _author;

    /// <summary>
    /// 作者
    /// </summary>
    public string Author
    {
    get { return _author; }
    set { _author = value; }
    }
    /// <summary>
    /// 页数
    /// </summary>
    private uint _page;

    /// <summary>
    /// 页数
    /// </summary>
    public uint Page
    {
    get { return _page; }
    set { _page = value; }
    }
    /// <summary>
    /// 价格
    /// </summary>
    private double _price;

    /// <summary>
    /// 价格
    /// </summary>
    public double Price
    {
    get { return _price; }
    set { _price = value; }
    }
    /// <summary>
    /// 备注
    /// </summary>
    private string _other;

    /// <summary>
    /// 备注
    /// </summary>
    public string Other
    {
    get { return _other; }
    set { _other = value; }
    }
    /// <summary>
    /// 书内容
    /// </summary>
    private List<string> _booklist;

    /// <summary>
    /// 书的内容
    /// </summary>
    public List<string> Booklist
    {
    get { return _booklist; }
    set { _booklist = value; }
    }

    /// <summary>
    /// 评论
    /// </summary>
    private string _bbs;

    /// <summary>
    /// 评论
    /// </summary>
    public string Bbs
    {
    get { return _bbs; }
    set { _bbs = value; }
    }
    public Book()
    {
    Booklist
    = new List<string>();
    }
    }
    class Program
    {
    static void Main(string[] args)
    {

    Program a
    = new Program();
    a.WriteBook();
    a.ReadBook();

    }
    /// <summary>
    /// 把自定义book的类序列化
    /// </summary>
    void WriteBook()
    {
    Book bk
    = new Book();//给书写属性
    bk.Name = "dfa";
    bk.Other
    = "asdf";
    bk.Page
    = 34;
    bk.Price
    = 4;
    ReadWrite wbook
    = new ReadWrite();//实例化自定义序列化类
    bool ok = wbook.Wbin(bk, "d:/mybook.bin"); //把book类序列化到本地文件并反回true\false
    if (ok)//判断是否序列化成功
    {
    Console.WriteLine(
    "序列化成功");
    }
    else
    {
    Console.WriteLine(
    "序列化失败");
    }
    }
    /// <summary>
    /// 把已序列化的文件反序列化
    /// </summary>
    void ReadBook()
    {
    Book rbk
    = new Book();
    ReadWrite rbook
    = new ReadWrite();//实例化自定义序列化类
    bool ok = rbook.Rbin("d:/mybook.bin", ref rbk);//把本地文件反序列化book类到并反回true\false,ref是带出序列化后的参数
    if (ok)//判断是否反序列化成功
    {
    Console.WriteLine(
    "读取成功");
    }
    else
    {
    Console.WriteLine(
    "读取失败");
    }
    Console.WriteLine(rbk.Name);
    Console.ReadKey();
    }
    }
    }
  • 相关阅读:
    jQuery轮播图(一)轮播实现并封装
    openSUSE 12.3 默认启动项
    最大堆(最小堆)
    二叉树基本操作续二:前序、中序、后序遍历(非递归 迭代方式)
    二叉树基本操作续一:二叉树建立、节点数统计
    二叉树基本操作:前序、中序、后序遍历(递归方式)
    Android如何打印std::cout/printf(重定向stdout)
    textarea高度跟随文字高度而变化
    箭头函数与普通函数的区别
    浏览器兼容问题
  • 原文地址:https://www.cnblogs.com/Witkey/p/1629244.html
Copyright © 2011-2022 走看看