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


    }

  • 相关阅读:
    SSO单点登录机制
    Web应用中Service层获取request对象 | RequestContextHolder的使用
    J2EE中数据字典的使用详解
    Redis高级(事务,持久化,主从复制读写分离,以及安全设置)
    Redis的五种数据类性以及对应的操作命令
    Redis客户端与基本命令
    VMware15安装CentOS8
    用内置的库turtle来画一朵花,python3
    python之经典猜数字
    python,寻找班级里面名字最长的人
  • 原文地址:https://www.cnblogs.com/jhabb/p/1881503.html
Copyright © 2011-2022 走看看