首页 > 代码库 > 使用泛型对读写配置文件的方法进行封装
使用泛型对读写配置文件的方法进行封装
一般一个站点会有多个配置文件,如果要针对每个配置文件进行读写,这是要疯的节奏
之前学泛型,一直没用到过,在这里练习一下,体会泛型实际对我们减少冗余代码的作用
/// <summary> /// 站点配置文件的路径 /// </summary> public class ConfigPath { /// <summary> /// 数据库配置文件 /// </summary> public static readonly string DB = "/App_Data/DB.config"; /// <summary> /// 站点配置文件 /// </summary> public static readonly string WebSite = "/App_Data/WebSite.config"; } /// <summary> /// 操作配置文件帮助类 /// </summary> public class ConfigHelper { private static object lockHelper = new object(); /// <summary> /// 加载配置文件 /// </summary> /// <param name="configPath">配置文件相对路径</param> /// <param name="IsCache">是否使用缓存</param> /// <param name="cacheKey">如果使用了缓存,则缓存键为</param> /// <returns></returns> public static T LoadConfig<T>(string configPath, bool isCache, string cacheKey) { if (isCache) { T model = (T)CacheHelper.Get(cacheKey); if (model == null) { model = (T)IOHelper.DeserializeFromXML(typeof(T), IOHelper.GetMapPath(configPath)); if (model != null) CacheHelper.Insert(cacheKey, model,IOHelper.GetMapPath(configPath)); } return model; } else { return (T)IOHelper.DeserializeFromXML(typeof(T), IOHelper.GetMapPath(configPath)); } } /// <summary> /// 写入配置文件 /// </summary> /// <param name="configPath">配置文件相对路径</param> /// <param name="obj">新的内容实体</param> /// <returns></returns> public static bool SaveConfig(string configPath,object obj) { var permissionSet = new PermissionSet(PermissionState.None); var writePermission = new FileIOPermission(FileIOPermissionAccess.Write, IOHelper.GetMapPath(configPath)); permissionSet.AddPermission(writePermission); if (permissionSet.IsSubsetOf(AppDomain.CurrentDomain.PermissionSet)) { lock (lockHelper) { IOHelper.SerializeToXml(obj,IOHelper.GetMapPath(configPath)); } return true; } else { return false;//没有写入权限 } } }
IOHelper类中序列化的两个方法:
#region 序列化 /// <summary> /// XML序列化 /// </summary> /// <param name="obj">序列对象</param> /// <param name="filePath">XML文件路径</param> /// <returns>是否成功</returns> public static bool SerializeToXml(object obj, string filePath) { bool result = false; FileStream fs = null; try { fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite); XmlSerializer serializer = new XmlSerializer(obj.GetType()); serializer.Serialize(fs, obj); result = true; } catch (Exception ex) { throw ex; } finally { if (fs != null) fs.Close(); } return result; } /// <summary> /// XML反序列化 /// </summary> /// <param name="type">目标类型(Type类型)</param> /// <param name="filePath">XML文件路径</param> /// <returns>序列对象</returns> public static object DeserializeFromXML(Type type, string filePath) { FileStream fs = null; try { fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); XmlSerializer serializer = new XmlSerializer(type); return serializer.Deserialize(fs); } catch (Exception ex) { throw ex; } finally { if (fs != null) fs.Close(); } } #endregion
配置文件的xml:
<?xml version="1.0" encoding="utf-8"?><WebSite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <SiteName>站点名称</SiteName> <SiteUrl>站点URL</SiteUrl> <ICP>备案信息</ICP> </WebSite>
实体类:
/// <summary> /// 站点配置信息 /// </summary> [Serializable] public class WebSite { /// <summary> /// 站点名称 /// </summary> public string SiteName{get;set;} /// <summary> /// 站点URL /// </summary> public string SiteUrl { get; set; } /// <summary> /// 站点配置信息 /// </summary> public string ICP { get; set; } }
调用:
/// <summary> /// 站点配置文件 /// </summary> public Model.WebSite SiteConfig { get { return ConfigHelper.LoadConfig<Model.WebSite>(ConfigPath.WebSite, true, CacheKey.CACHE_SITECONFIG); } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。