zoukankan      html  css  js  c++  java
  • 序列化:反序列化为另一个对象

    将类型Person序列化为文件后,反序列化该文件为另一个类型PersonAnother,以下是相关代码:
     
    using System;
    using System.Runtime.Serialization;

    namespace ConsoleApp_SerializableToAnotherObj
    {
    class Program
    {
    static void Main(string[] args)
    {
    //反序列化为另一个对象
    Person personA=new Person(){FirstName = "Zhang",LastName = "San"};
    BinarySerializer.SerializeToFile(personA,@"c:","person.txt");
    PersonAnother p = BinarySerializer.DeserializeFromFile<PersonAnother>(@"
    c:person.txt");
    Console.WriteLine(p.Name);
    Console.ReadKey();
    }
    }

    [Serializable]
    class PersonAnother : ISerializable
    {
    public string Name { get; set; }

    protected PersonAnother(SerializationInfo info, StreamingContext context)
    {
    Name = info.GetString("
    Name");
    }

    void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
    {
    }
    }

    [Serializable]
    class Person : ISerializable
    {
    public string FirstName;
    public string LastName;
    public string ChineseName;

    public Person()
    {
    }
    protected Person(SerializationInfo info, StreamingContext context)
    { }

    void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
    {
    info.SetType(typeof(PersonAnother));
    info.AddValue("
    Name", string.Format("{0} {1}", LastName, FirstName));
    }
    }
    }


    工具类:
    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;

    namespace ConsoleApp_SerializableToAnotherObj
    {
    public class BinarySerializer
    {
    /// <summary>
    /// 将类型序列化为字符串
    /// </summary>
    public static string Serialize<T>(T t)
    {
    using (MemoryStream stream = new MemoryStream())
    {
    BinaryFormatter formatter = new BinaryFormatter();
    formatter.Serialize(stream, t);
    return System.Text.Encoding.UTF8.GetString(stream.ToArray());
    }
    }

    /// <summary>
    /// 将类型序列化为文件
    /// </summary>
    public static void SerializeToFile<T>(T t, string path, string fullname)
    {
    if (!Directory.Exists(path))
    {
    Directory.CreateDirectory(path);
    }
    string fullPath = string.Format(@"{0}{1}", path, fullname);
    using (FileStream stream = new FileStream(fullPath, FileMode.OpenOrCreate))
    {
    BinaryFormatter formatter = new BinaryFormatter();
    formatter.Serialize(stream, t);
    stream.Flush();
    }
    }

    /// <summary>
    /// 将字符串反序列化为类型
    /// </summary>
    public static TResult Deserialize<TResult>(string s) where TResult : class
    {
    byte[] bs = System.Text.Encoding.UTF8.GetBytes(s);
    using (MemoryStream stream = new MemoryStream(bs))
    {
    BinaryFormatter formatter = new BinaryFormatter();
    return formatter.Deserialize(stream) as TResult;
    }
    }

    /// <summary>
    /// 将文件反序列化为类型
    /// </summary>
    public static TResult DeserializeFromFile<TResult>(string path) where TResult : class
    {
    using (FileStream stream = new FileStream(path, FileMode.Open))
    {
    BinaryFormatter formatter = new BinaryFormatter();
    return formatter.Deserialize(stream) as TResult;
    }
    }
    }
    }

  • 相关阅读:
    Response.Redirect、Server.Transfer、Server.Execute的区别
    js删除Array指定位置元素方法
    用Json.NET将json字符串反序列化为json匿名对象
    Asp.net中编程方式调用ashx(通过webRequest)
    Server.Transfer中传递ViewState方法
    Ext.Net中Ext.net.DirectMethods无法找到DirectMethod
    IFrame网页加载完成事件
    oracle中grant授权说明
    深度剖析Byteart Retail案例:前言
    深度剖析Byteart Retail案例:仓储(Repository)及其上下文(Repository Context)
  • 原文地址:https://www.cnblogs.com/jx270/p/3659982.html
Copyright © 2011-2022 走看看