zoukankan      html  css  js  c++  java
  • 单向序列化

    今天遇到一个问题就是DataGridViewCellStyle这个类型,我们想对其进行序列化。但是遗憾的是,该类型并没有声明为可序列化。所以,不管我们用哪一个序列化器,都会报告错误。似乎这是一个不可能解决的问题。

    经过研究,在.NET 2.0中确实比较难做到,但可以通过.NET 3.0的一个DataContractSerializer来实现。该序列化器是专门为WCF设计的,是XML序列化器的一种。

    http://msdn.microsoft.com/zh-cn/vstudio/system.runtime.serialization.datacontractserializer.aspx

    下面是一个例子:

    创建一个windows Forms程序,选择.NET Framework 为3.0,并要添加System.Runtime.Serialization程序集的引用。

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using System.Runtime.Serialization;
    using System.IO;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            /// <summary>
            /// 序列化
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void Serialize_Click(object sender, EventArgs e)
            {
                //因为DataGridViewCellStyle的属性有一些特殊类型,所以要预先注册
                List<Type> types = new List<Type>();
                types.Add(typeof(DBNull));
                types.Add(typeof(System.Globalization.CultureInfo));
    
    
                DataContractSerializer x = new 
                    DataContractSerializer(typeof(DataGridViewCellStyle), types);
                DataGridViewCellStyle style = new DataGridViewCellStyle();
                style.BackColor = System.Drawing.Color.Red;
    
                FileStream fs = new FileStream("temp.xml", FileMode.Create);
                x.WriteObject(fs, style);
                fs.Close();
    
            }
    
            /// <summary>
            /// 反序列化
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void DeSerialize_Click(object sender, EventArgs e)
            {
                List<Type> types = new List<Type>();
                types.Add(typeof(DBNull));
                types.Add(typeof(System.Globalization.CultureInfo));
    
    
                DataContractSerializer x = new 
                    DataContractSerializer(typeof(DataGridViewCellStyle), types);
                FileStream fs = new FileStream("temp.xml", FileMode.Open);
                DataGridViewCellStyle style = (DataGridViewCellStyle)x.ReadObject(fs);
                fs.Close();
                MessageBox.Show(style.BackColor.ToString());
    
            }
        }
    }
    image 
    再深入地看,我们可以用NetDataContractSerializer来实现同样的目的
    http://msdn.microsoft.com/zh-cn/vstudio/system.runtime.serialization.netdatacontractserializer.aspx
    序列化
    DataGridViewCellStyle style = new DataGridViewCellStyle();
    style.BackColor = System.Drawing.Color.Red; 
    
    FileStream fs = new FileStream("temp.xml", FileMode.Create);
    NetDataContractSerializer x = new NetDataContractSerializer();
    x.Serialize(fs, style);
    fs.Close();
    
    反序列化
    FileStream fs = new FileStream("temp.xml", FileMode.Open);
    NetDataContractSerializer x = new NetDataContractSerializer();
    DataGridViewCellStyle style = (DataGridViewCellStyle)x.Deserialize(fs);
    fs.Close();
    MessageBox.Show(style.BackColor.ToString());
  • 相关阅读:
    PC逆向之代码还原技术,第六讲汇编中除法代码还原以及原理第一讲,除数是2的幂
    PC逆向之代码还原技术,第五讲汇编中乘法的代码还原
    PC逆向之代码还原技术,第四讲汇编中减法的代码还原
    【Android】20.2 视频播放
    【Android】20.1 音频播放
    【Android】20.0 第20章 音频、视频、拍照、语音合成
    【Android】常见问题解答
    【Android】19.3 ContentProvider及安卓进一步封装后的相关类
    【Android】19.2 ShareActionProvider类—帮你把信息分享出去
    【Android】19.1 SharedPreferences类
  • 原文地址:https://www.cnblogs.com/chenxizhang/p/1286345.html
Copyright © 2011-2022 走看看