国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

自定義ConfigurationSection,創建多個嵌套的ConfigurationElementCollection節點

2019-11-17 01:48:06
字體:
來源:轉載
供稿:網友

自定義ConfigurationSection,創建多個嵌套的ConfigurationElementCollection節點

本人現在在一家游戲公司,最近在做一個項目,需要做一個GM的管理后臺,需要調用其他公司提供的接口,來實現后臺管理的操作

由于接口地址都是固定的,所以想到使用自定義節點,來將接口都配置到web.config中。

很快,v1.0版本出爐: 

public class RequestConfigSection : ConfigurationSection    {        [Configuration

在web.config中的配置方式為:

<apiRequestConfig>  <sources>    <add name="..." url="..." type="POST" />    <add name="..." url="..." type="POST" />    <add name="..." url="..." type="POST" />  </sources></apiRequestConfig>

這時候又看了一遍需求文檔,發現有說明不同平臺的接口地址是不一樣的,但接口做的事情是一樣的。

然后就開始想,如果接著在下邊追加,則不同平臺的同一接口的名稱是不能相同的。

所以想到的理想的配置方式為:

<apiRequestConfig>  <sources platform="android">    <add name="..." url="..." type="POST" />    <add name="..." url="..." type="POST" />    <add name="..." url="..." type="POST" />  </sources>  <sources platform="ios">    <add name="..." url="..." type="POST" />    <add name="..." url="..." type="POST" />    <add name="..." url="..." type="POST" />  </sources></apiRequestConfig>

但是sources 名稱的節點只能出現一次&hellip;好吧,蛋疼了。

研究嘗試了一上午也沒有找到合適的解決方式,又懶得再重新寫一套代碼來讀取xml,…開始在網上搜解決方案

用中文做關鍵字找不著…翻了墻,用英文來當關鍵字 one or more ConfigurationElementCollection…

最終在一老外的博客里找到了一個替代的解決方案,最終的配置為:

<apiRequestConfig>    <requestConfigs>      <request platform="android">        <sources>          <add name="..." url="..." type="POST" />          <add name="..." url="..." type="POST" />          <add name="..." url="..." type="POST" />        </sources>      </request>      <request platform="ios">        <sources>          <add name="..." url="..." type="POST" />          <add name="..." url="..." type="POST" />          <add name="..." url="..." type="POST" />        </sources>      </request>    </requestConfigs>  </apiRequestConfig>

C#代碼如下:

public class RequestConfigSection : ConfigurationSection    {        [ConfigurationProperty("requestConfigs", IsDefaultCollection = true)]        [ConfigurationCollection(typeof(RequestConfigTypeCollection), AddItemName = "request")]        public RequestConfigTypeCollection ConfigCollection        {            get { return (RequestConfigTypeCollection)this["requestConfigs"]; }            set { this["requestConfigs"] = value; }        }        /// <summary>        /// 根據平臺和名稱獲取請求配置信息        /// </summary>        /// <param name="name"></param>        /// <param name="platform"></param>        /// <returns></returns>        public RequestConfigSource GetRequestConfigSource(string platform, string name)        {            return ConfigCollection[platform].SourceCollection[name];        }    }    public class RequestConfigTypeCollection : ConfigurationElementCollection    {        /// <summary>        /// 創建新元素        /// </summary>        /// <returns></returns>        protected override ConfigurationElement CreateNewElement()        {            return new RequestConfigType();        }        /// <summary>        /// 獲取元素的鍵        /// </summary>        /// <param name="element"></param>        /// <returns></returns>        protected override object GetElementKey(ConfigurationElement element)        {            return ((RequestConfigType)element).Platform;        }        /// <summary>        /// 獲取所有鍵        /// </summary>        public IEnumerable<string> AllKeys { get { return BaseGetAllKeys().Cast<string>(); } }        /// <summary>        /// 索引器        /// </summary>        /// <param name="name"></param>        /// <returns></returns>        public new RequestConfigType this[string platform]        {            get { return (RequestConfigType)BaseGet(platform); }        }    }    public class RequestConfigType : ConfigurationElement    {        /// <summary>        /// 獲取全部請求配置信息        /// </summary>        /// <returns></returns>        public RequestConfigSource[] GetAllRequestSource()        {            var keys = this.SourceCollection.AllKeys;            return keys.Select(name => this.SourceCollection[name]).ToArray();        }        /// <summary>        /// 平臺標識        /// </summary>        [ConfigurationProperty("platform")]        public string Platform        {            get { return (string)this["platform"]; }            set { this["platform"] = value; }        }        [ConfigurationProperty("sources", IsDefaultCollection = true)]        [ConfigurationCollection(typeof(RequestConfigSourceCollection), AddItemName = "add")]        public RequestConfigSourceCollection SourceCollection        {            get { return (RequestConfigSourceCollection)this["sources"]; }            set { this["sources"] = value; }        }    }    public class RequestConfigSourceCollection : ConfigurationElementCollection    {        /// <summary>        /// 創建新元素        /// </summary>        /// <returns></returns>        protected override ConfigurationElement CreateNewElement()        {            return new RequestConfigSource();        }        /// <summary>        /// 獲取元素的鍵        /// </summary>        /// <param name="element"></param>        /// <returns></returns>        protected override object GetElementKey(C
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 辽宁省| 丽水市| 兰溪市| 河池市| 财经| 海原县| 桂东县| 阜阳市| 休宁县| 秦安县| 铜梁县| 武鸣县| 南汇区| 博罗县| 油尖旺区| 沁水县| 丘北县| 清徐县| 连城县| 秦皇岛市| 曲沃县| 凌海市| 瑞昌市| 宜丰县| 清水河县| 南充市| 金华市| 昭平县| 台南县| 信丰县| 安康市| 大丰市| 华蓥市| 崇州市| 灵寿县| 澜沧| 维西| 焦作市| 故城县| 庄浪县| 蚌埠市|