How to Read a Configuration Section from XML in a Database?

后端 未结 5 1620
失恋的感觉
失恋的感觉 2021-02-01 04:01

I have a Config class like this:

public class MyConfig : ConfigurationSection
{
        [ConfigurationProperty(\"MyProperty\", IsRequired = true)]
        public         


        
5条回答
  •  梦如初夏
    2021-02-01 04:58

    My suggestion would be to keep your current MyConfig class but load your XML from your database in the constructor, then in each property of your MyConfig, you can put in logic to determine where you get the value from (either database or .config file) if you need to pull config from either location, or have it fall back if the value is empty.

    public class MyConfig : ConfigurationSection
    {
        public MyConfig()
        {
            // throw some code in here to retrieve your XML from your database
            // deserialize your XML and store it 
            _myProperty = "";
        }
    
        private string _myProperty = string.Empty;
    
        [ConfigurationProperty("MyProperty", IsRequired = true)]
        public string MyProperty
        {
            get
            {
                if (_myProperty != null && _myProperty.Length > 0)
                    return _myProperty;
                else
                    return (string)this["MyProperty"];
            }
            set { this["MyProperty"] = value; }
        }
    }
    

提交回复
热议问题