c# 对序列化类XMLSerializer 二次封装泛型

加工的泛型类如下:

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
    {

        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;
        }
    }
}

例如我们有个简单的类



class Apply

{

[CategoryAttribute("基本参数"), DisplayName("片数")]

public int WaferNum { get; set; } = 25;

[CategoryAttribute("基本参数"), DisplayName("文件原路径")]

public string OriFilePath { get; set; } = "D:Data";

}



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

Apply apply = new Apply();

XMLSerializer.Save(apply , "D:Appply.xml");

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

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

class App

{

int a = 0;

public App Clone()

{

App a;

a = XMLSerializer.Clone(this);

return a;

}

}

展开阅读全文

页面更新:2024-05-12

标签:基本参数   路径   加工   简单   文件

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号

Top