C#序列化与反序列化操作

[Serializable]
    public class BillPropertyConfig
    {
        public BillPropertyConfig()
        {
            this.PlayItemBackCol = Color.Red;
            this.PlayItemFontCol = Color.Blue;
            this.FontSize = new Font("宋体", 10);
            this.FontOriSize = new Font("宋体", 10);
            this.Show_start = true;
            this.Show_model = true;
            this.Show_set = true;
            this.Show_title = true;
            this.Show_in = true;
            this.Show_dur = true;
            this.Show_you = true;
            this.Show_gua = true;
            this.Show_gua2 = true;
            this.Show_tai = true;
            this.Show_ju = true;
            this.Show_dao = true;
            this.Show_signal = true;
            this.Show_type = true;
            this.Show_department = true;
            this.Show_mappingMode = true;
        }
        [XmlIgnore]
        public Color PlayItemBackCol { get; set; }
        public int SerializerPlayItemBackCol
        {
            get
            {
                return ColorTranslator.ToWin32(PlayItemBackCol);
            }
            set
            {
                PlayItemBackCol = ColorTranslator.FromWin32(value);
            }
        }
        [XmlIgnore]
        public Color PlayItemFontCol { get; set; }
        public int SerializerPlayItemFontCol
        {
            get
            {
                return ColorTranslator.ToWin32(PlayItemFontCol);
            }
            set
            {
                PlayItemFontCol = ColorTranslator.FromWin32(value);
            }
        }
        [XmlIgnore]
        public Font FontSize { get; set; }
        public myFont SerializerFontSize
        {
            get
            {
                myFont _fontSize = new myFont();


                _fontSize.Name = FontSize.Name;
                _fontSize.Size = FontSize.Size;
                _fontSize.Style = FontSize.Style;


                return _fontSize;


            }


            set
            {
                FontSize = new Font(value.Name, value.Size, value.Style);
            }
        }
        [XmlIgnore]
        public Font FontOriSize { get; set; }
        public myFont SerializerFontOriSize
        {
            get
            {
                myFont _fontOriSize = new myFont();


                _fontOriSize.Name = FontOriSize.Name;
                _fontOriSize.Size = FontOriSize.Size;
                _fontOriSize.Style = FontOriSize.Style;


                return _fontOriSize;


            }


            set
            {
                FontOriSize = new Font(value.Name, value.Size, value.Style);
            }
        }
        public bool Show_start { get; set; }
        public bool Show_model { get; set; }
        public bool Show_set { get; set; }
        public bool Show_title { get; set; }
        public bool Show_in { get; set; }
        public bool Show_dur { get; set; }
        public bool Show_you { get; set; }
        public bool Show_gua { get; set; }
        public bool Show_gua2 { get; set; }
        public bool Show_tai { get; set; }
        public bool Show_ju { get; set; }
        public bool Show_dao { get; set; }
        public bool Show_signal { get; set; }
        public bool Show_type { get; set; }
        public bool Show_department { get; set; }
        public bool Show_mappingMode { get; set; }


        public string ToXML()
        {
            return StaticMethod.Obj2Xml(this);
        }


        public BillPropertyConfig FromXML(string str)
        {
            BillPropertyConfig m = StaticMethod.Xml2Obj(str, this.GetType()) as BillPropertyConfig;
            if (m != null)
            {
                return m;
            }
            return new BillPropertyConfig();
        }
    }
    [Serializable]
    public class myFont
    {
        public myFont()
        {
        }
        public string Name;
        public FontStyle Style;//这个可以包括其他几项。
        public float Size;

    }

 public static string Obj2Xml(object obj)
        {
            string xmlStr = "";
            try
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    XmlSerializer xml = new XmlSerializer(obj.GetType());
                    xml.Serialize(ms, obj);
                    byte[] arr = ms.ToArray();
                    xmlStr = Encoding.UTF8.GetString(arr);
                }
            }
            catch { }
            return xmlStr;
        }


        public static object Xml2Obj(string str, Type t)
        {
            if (str == "")
                return null;
            object obj = null;
            try
            {
                using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(str)))
                {
                    //XmlSerializer xml = XmlSerializer.FromTypes(new[] { t }).FirstOrDefault(); ;
                    XmlSerializer xml = new XmlSerializer(t);
                    obj = xml.Deserialize(ms);
                }
            }
            catch { }
            return obj;
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#中,序列化是将对象转换为字节流的过程,而反序列化是将字节流转换回对象的过程。C#提供了多种序列化反序列化的方式,其中最常用的是使用BinaryFormatter类进行二进制序列化反序列化。该类可以将对象序列化为简洁的字节流,并能够通过相同的格式化程序进行反序列化。 在序列化一个对象时,BinaryFormatter会将对象的类名、程序集和所有数据成员写入存储中。它还会处理对象之间的引用关系,确保同一对象不会被多次序列化。需要注意的是,被序列化的对象引用的所有对象都必须标记为Serializable,否则在序列化未标记的对象时会引发异常。 反序列化时,BinaryFormatter会根据序列化时写入的对象信息,将字节流转换回对象。反序列化过程中不会调用构造函数,这是为了性能考虑所做的约束。 要使用BinaryFormatter进行序列化反序列化,您需要创建一个流的实例和一个BinaryFormatter实例,并使用Serialize方法将对象序列化为字节流,将流和对象作为参数传递给Serialize方法。 需要注意的是,C#还提供了其他序列化反序列化的方式,例如XML序列化和JSON序列化,它们分别使用XmlSerializer类和JsonSerializer类。这些类对于特定的序列化需求可能更加适用,但在一般情况下,使用BinaryFormatter是一种简单且高效的方式进行对象的序列化反序列化。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [C# - 序列化反序列化](https://blog.csdn.net/dodream/article/details/4729415)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值