c# 对序列化类XMLSerializer 二次封装泛型化方便了一些使用的步骤

2023-03-31 12:47:04 浏览数 (1)

原文作者:aircraft

  原文链接:https://cloud.tencent.com/developer/article/2255193

加工的泛型类如下:

代码语言:javascript复制
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace Data
{
    public class XMLSerializer<T>
    {

        public static bool Save(T obj, string flieName)
        {
            string dir = Path.GetDirectoryName(flieName);
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);
            try
            {
                if (flieName.Trim().Length == 0)
                    return false;
                string strFolder = Path.GetDirectoryName(flieName);

                XmlSerializer xs = new XmlSerializer(typeof(T));
                using (FileStream fs = new FileStream(flieName, FileMode.Create))
                {
                    xs.Serialize(fs, obj);
                    //fs.Close ();
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("序列化保存时出错,出错原因为:"   ex.ToString());
                return false;
            }
        }

        public static T Load(string fileName)
        {
            if (File.Exists(fileName) == false)
            {
                return default(T);
            }

            T obj = default(T);
            try
            {
                XmlSerializer xml = new XmlSerializer(typeof(T));
                
                using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    obj = (T)xml.Deserialize(fs);
                    //fs.Close ();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("序列化读取时出错,出错原因为:"   ex.ToString());
                return default(T);
            }
            return obj;
        }

        public static T Clone(T target)
        {
            T obj = default (T);
            try
            {
                MemoryStream ms = new MemoryStream ();
                XmlSerializer xml = new XmlSerializer (typeof (T));
                xml.Serialize (ms, target);
                ms.Seek (0, SeekOrigin.Begin);
                obj = (T)xml.Deserialize (ms);
            }
            catch (Exception ex)
            {
                MessageBox.Show ("拷贝时出错,出错原因为:"   ex.ToString ());
                return default (T);
            }
            return obj;
        }
    }
}

例如我们有个简单的类

代码语言:javascript复制
 class Apply
    {
        [CategoryAttribute("基本参数"), DisplayName("片数")]
        public int WaferNum { get; set; } = 25;
        [CategoryAttribute("基本参数"), DisplayName("文件原路径")]
        public string OriFilePath { get; set; } = "D:\Data";

    }

需要去导入,保存,深拷贝复制,我们就可以这样调用

代码语言:javascript复制
Apply apply = new Apply();

XMLSerializer<Apply>.Save(apply , "D:\Appply.xml");



Apply apply = XMLSerializer<Apply>.Load( "D:\Appply.xml");


//也可以作为类的复制快捷方式来使用--例如下面类这样
class App
        {

            int a = 0;

            public App Clone()
            {
                App a;
                a = XMLSerializer<App>.Clone(this);
                return a;
            }


        }

0 人点赞