欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

自定義ConfigurationSection,創建多個嵌套的Co

系統 1650 0

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

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

很快,v1.0版本出爐: 

      public class RequestConfigSection : ConfigurationSection

    {

        [ConfigurationProperty("sources", IsDefaultCollection = true)]

        [ConfigurationCollection(typeof(RequestConfigSourceCollection), AddItemName = "add")]

        public RequestConfigSourceCollection ConfigCollection

        {

            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(ConfigurationElement element)

        {

            return ((RequestConfigSource)element).Name;

        }



        /// <summary>

        /// 獲取所有鍵

        /// </summary>

        public IEnumerable<string> AllKeys { get { return BaseGetAllKeys().Cast<string>(); } }



        /// <summary>

        /// 索引器

        /// </summary>

        /// <param name="name"></param>

        /// <returns></returns>

        public new RequestConfigSource this[string name]

        {

            get { return (RequestConfigSource)BaseGet(name); }

        }

    }



    public class RequestConfigSource : ConfigurationElement

    {

        /// <summary>

        /// 名稱

        /// </summary>

        [ConfigurationProperty("name")]

        public string Name

        {

            get { return (string)this["name"]; }

            set { this["name"] = value; }

        }



        /// <summary>

        /// 地址

        /// </summary>

        [ConfigurationProperty("url")]

        public string Url

        {

            get { return (string)this["url"]; }

            set { this["url"] = value; }

        }



        /// <summary>

        /// 訪問類型

        /// </summary>

        [ConfigurationProperty("type")]

        public RequestType RequestType

        {

            get

            {

                return (RequestType)Enum.Parse(typeof(RequestType), this["type"].ToString(), true);

            }

            set { this["type"] = value; }

        }

    }


    

?

在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 名稱的節點只能出現一次…好吧,蛋疼了。

研究嘗試了一上午也沒有找到合適的解決方式,又懶得再重新寫一套代碼來讀取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(ConfigurationElement element)

        {

            return ((RequestConfigSource)element).Name;

        }



        /// <summary>

        /// 獲取所有鍵

        /// </summary>

        public IEnumerable<string> AllKeys { get { return BaseGetAllKeys().Cast<string>(); } }



        /// <summary>

        /// 索引器

        /// </summary>

        /// <param name="name"></param>

        /// <returns></returns>

        public new RequestConfigSource this[string name]

        {

            get { return (RequestConfigSource)BaseGet(name); }

        }

    }



    /// <summary>

    /// 請求的配置信息

    /// </summary>

    public class RequestConfigSource : ConfigurationElement

    {

        /// <summary>

        /// 名稱

        /// </summary>

        [ConfigurationProperty("name")]

        public string Name

        {

            get { return (string)this["name"]; }

            set { this["name"] = value; }

        }



        /// <summary>

        /// 地址

        /// </summary>

        [ConfigurationProperty("url")]

        public string Url

        {

            get { return (string)this["url"]; }

            set { this["url"] = value; }

        }



        /// <summary>

        /// 訪問類型

        /// </summary>

        [ConfigurationProperty("type")]

        public RequestType RequestType

        {

            get

            {

                return (RequestType)Enum.Parse(typeof(RequestType), this["type"].ToString(), true);

            }

            set { this["type"] = value; }

        }

    }


    

?

本人的開發環境為 .net framework 4.0

最初 RequestConfigSection 類中的 ConfigCollection 和? RequestConfigType 類中的 SourceCollection ? 沒有定義 ConfigurationCollection 特性

而是在 RequestConfigTypeCollection RequestConfigTypeCollection 中重載了 ElementName 屬性,返回子級的節點名。

結果拋出節點名未定義的異?!?

改由特性 ConfigurationCollection 定義,并給特性屬性 AddItemName 賦值為子級的節點名 解決…

?

老外博客地址: http://tneustaedter.blogspot.com/2011/09/how-to-create-one-or-more-nested.html ? 需要FQ訪問

或者直接看這兒,俺給Copy出來了: http://www.cnblogs.com/efenghuo/articles/4022836.html

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


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 久久亚洲私人国产精品 | 日本美女毛茸茸 | 黄色一级a毛片 | 6全高清智能录播系统视频 精品九九 | 欧美激情欧美激情在线五月 | 最新日本中文字幕在线观看 | 天天看片天天a免费观看 | 欧美激情人成日本在线视频 | 91专区在线观看 | 综合网婷婷 | 国产日韩欧美不卡 | 国产国产精品人在线观看 | 国产日韩视频 | 欧美一级毛片高清免费观看 | 日韩在线免费播放 | 精品视频久久 | 精品日本一区二区 | 成人啪啪97丁香 | 日韩精品小视频 | 国产成人精品免费视频大全最热 | 极品尤物一区二区三区 | 91中文字幕在线观看 | 精品久久一区二区 | 九九黄色 | 国产在视频一区二区三区吞精 | 国产欧美日韩 | 亚洲精品国产精品国自产观看 | 97精品国产91久久久久久 | 日本高清在线中文字幕网 | 欧美成人26uuu欧美毛片 | jizzjizz视频 | 欧美一区二区三区成人精品 | 久久久久成人精品免费播放 | 奇米影视图片 | 91最新免费观看在线 | 日日爱视频 | 色接久久 | 久久精品一区二区国产 | 一级色毛片 | 国产日产在线观看 | 亚洲精品久久九九热 |