zoukankan      html  css  js  c++  java
  • 应用程序xml 配置文件抽象基类

    using System;
    using System.Text;
    using System.Xml.Serialization;
    using System.IO;
    using System.Reflection;

    namespace IpTerm
    {

    //版权所有: jhabb  邮箱:jhabb@163.com//   qq :75420724      欢迎讨论 转载请标明
        /// <summary>
        /// 应用程序参数配置抽象基类
        /// </summary>
        public abstract class ConfigurationBase
        {
            /// <summary>
            /// 构造函数
            /// </summary>
            public ConfigurationBase()
            { }

            /// <summary>
            /// 反序列化
            /// </summary>
            public void Load(string fileName)
            {
                if (string.IsNullOrEmpty(fileName)) { return; }

                TextReader textReader = null;
                XmlSerializer serial = null;
                ConfigurationBase configuration = null;
                if (!File.Exists(fileName)) { Save(fileName); return; }
                try
                {
                    textReader = new StreamReader(fileName);
                    serial = new XmlSerializer(this.GetType());
                    configuration = serial.Deserialize(textReader) as ConfigurationBase;
                    textReader.Close();
                    foreach (PropertyInfo propertyInfo in this.GetType().GetProperties())
                    {
                        if (propertyInfo.CanWrite)
                        {
                            object value = this.GetType().GetProperty(propertyInfo.Name).GetValue(configuration, null);
                            propertyInfo.SetValue(this, value, null);
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.Out.WriteLine(e.Message);
                }
                finally
                {
                    if (textReader != null) { textReader.Close(); }
                }
            }

            /// <summary>
            ///  序列化
            /// </summary>
            public void Save(string fileName)
            {
                if (string.IsNullOrEmpty(fileName)) { return; }
                string directoryName = Path.GetDirectoryName(fileName);
                if (!string.IsNullOrEmpty(directoryName) && !Directory.Exists(directoryName))
                {
                    DirectoryInfo dirInfo;
                    try
                    {
                        dirInfo = Directory.CreateDirectory(Path.GetDirectoryName(fileName));
                    }
                    catch (Exception e)
                    {
                        Console.Out.WriteLine(e.Message);
                        return;
                    }
                }

                TextWriter textWriter = null;
                XmlSerializer serial = null;
                try
                {
                    textWriter = new StreamWriter(fileName);
                    serial = new XmlSerializer(this.GetType());
                    serial.Serialize(textWriter, this);
                }
                catch (Exception e)
                {
                    Console.Out.WriteLine(e.Message);
                }
                finally
                {
                    if (textWriter != null) { textWriter.Close(); }
                }
            }
        }


    }

  • 相关阅读:
    LVS-NAT模式的配置详解
    流处理与消息队列------《Designing Data-Intensive Applications》读书笔记16
    流式计算与计算抽象化------《Designing Data-Intensive Applications》读书笔记15
    MapReduce与批处理------《Designing Data-Intensive Applications》读书笔记14
    分布式系统的一致性算法------《Designing Data-Intensive Applications》读书笔记13
    线性一致性与全序广播------《Designing Data-Intensive Applications》读书笔记12
    分布式系统的烦恼------《Designing Data-Intensive Applications》读书笔记11
    猫眼电影和电影天堂数据csv和mysql存储
    爬虫——requests.get爬虫模块参数
    机器学习——数据预处理
  • 原文地址:https://www.cnblogs.com/jhabb/p/1881503.html
Copyright © 2011-2022 走看看