C#winform控件序列化,反序列化

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Windows.Forms;
using System.Threading.Tasks;

namespace WF.Serialize
{
    /*
     * 序列化控件核心代码
     * 
     */
    [Serializable]
    public class Label_x : Label, ISerializable
    {
        public Label_x() { }

        ///   <summary> 
        ///   反序列化构造函数 
        ///   </summary> 
        ///   <param   name= "info "> </param> 
        ///   <param   name= "context "> </param> 
        public Label_x(SerializationInfo info, StreamingContext context)
        {
            this.SetObjectDataEx(info, context);
            this.BorderStyle = (BorderStyle)info.GetValue("BorderStyle", typeof(BorderStyle));

            //新加的属性放新线程里,因为缓存数据没有该字段GetValue会报错
            //放新线程里面,报错不会影响主线程
            //可能会照成加载变慢,该控件越多越慢
            //打印或保存模板,会存到缓存,下次打开就不会慢
            //Task.Factory.StartNew(new Action(() => {

                try
                {
                    this.TextAlign = (ContentAlignment)info.GetValue("TextAlign", typeof(ContentAlignment));
                }
                catch 
                {
                }

            //}));

           
        }
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            this.GetObjectDataEx(info, context);
            info.AddValue("BorderStyle", this.BorderStyle);
            info.AddValue("TextAlign", this.TextAlign);
        }
    }


    [Serializable]
    public class TextBox_x : TextBox, ISerializable
    {
        public TextBox_x() { }
        public TextBox_x(SerializationInfo info, StreamingContext context)
        {
            this.SetObjectDataEx(info, context);
            this.BorderStyle = (BorderStyle)info.GetValue("BorderStyle", typeof(BorderStyle));
        }
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            this.GetObjectDataEx(info, context);
            info.AddValue("BorderStyle", this.BorderStyle);
        }
    }

    [Serializable]
    public class Pane_x : Panel, ISerializable
    {
        public Pane_x() { }
        public Pane_x(SerializationInfo info, StreamingContext context)
        {
            this.SetObjectDataEx(info, context);

        }
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            this.GetObjectDataEx(info, context);

            info.AddValue("BorderStyle", this.BorderStyle);

        }
    }

    /// <summary>
    /// 序列化管理类
    /// </summary>
    public static class SerializeManager
    {


        public static List<Control> _controls = new List<Control>();

        static BinaryFormatter form = new BinaryFormatter();
        /// <summary>
        /// 序列化保存到文件
        /// </summary>
        /// <param name="serializable"></param>
        /// <param name="path"></param>
        public static void SerializeSave(this ISerializable serializable, string path)
        {
            using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write))
            {
                form.Serialize(fs, serializable);
            }
        }

        /// <summary>
        /// 反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="path"></param>
        /// <returns></returns>
        public static T DeSerialize<T>(string path) where T : class, ISerializable
        {
            _controls.Clear();
            using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                return form.Deserialize(fs) as T;
            }
        }
        public static void SetObjectDataEx(this ISerializable serializable, SerializationInfo info, StreamingContext context)
        {
            var control = serializable as Control;
            control.Name = info.GetString("Name")==""? Guid.NewGuid().ToString(): info.GetString("Name");
            control.Size = (Size)info.GetValue("Size", typeof(Size));
            control.Location = (Point)info.GetValue("Location", typeof(Point));
            control.Font = (Font)info.GetValue("Font", typeof(Font));
            control.Text = (string)info.GetValue("Text", typeof(string));
            control.Tag = info.GetValue("Tag", typeof(object));
            control.BackColor = (Color)info.GetValue("BackColor", typeof(Color));
            control.ForeColor = (Color)info.GetValue("ForeColor", typeof(Color));
            var controls = (List<ISerializable>)info.GetValue("Controls", typeof(List<ISerializable>));
            control.Controls.AddRange(controls.Cast<Control>().ToArray());
            _controls.Add(control);
        }

        public static void GetObjectDataEx(this ISerializable serializable, SerializationInfo info, StreamingContext context)
        {
            var control = serializable as Control;
            info.AddValue("Name", control.Name);
            info.AddValue("Size", control.Size);
            info.AddValue("Location", control.Location);
            info.AddValue("Font", control.Font);
            info.AddValue("Text", control.Text);
            info.AddValue("Tag", control.Tag);
            info.AddValue("BackColor", control.BackColor);
            info.AddValue("ForeColor", control.ForeColor);

            var controls = new List<ISerializable>();
            foreach (var item in control.Controls.Cast<Control>())
            {
                if (item is ISerializable)
                {
                    controls.Add((item as ISerializable));
                }
            }
            info.AddValue("Controls", controls);
        }
    }
}

 

posted @ 2021-11-15 14:15  212的s  阅读(805)  评论(0编辑  收藏  举报