C # Object-oriented way to set, read application configuration

Source: Internet
Author: User

Focus Point :

  • 1. Access and set configuration items in an object-oriented manner (Get,set)
  • 2. Workarounds for "callermembername" under. NET 4

The last week, everyone has entered the New Year mode. The body is still, the heart is far away. Recently things are not many, always look at the flash is not right AH. So I remembered a few days ago to see an article: "In. NET, a more convenient way to manipulate configuration items "; so try. As a result of a problem, the article uses the Callermembername property to simplify access to configuration items that require hard-coded key with the configuration item, which is used to tag the property name automatically at run time, but this is more than. NET 4.5. Since we do industrial control needs to be compatible with the old operating system of IPC,. NET has been 4.0. The compatibility issue was met. Baidu came out, found Yang Zhengko Teacher's approach is to use StackTrace specific is: "New StackTrace (true)." GetFrame (1). GetMethod (). Name ". The classes in the article were then modified:

 Public abstract class Configsetting:inotifypropertychanged {private void notifypropertychanged (string proper ty) {propertychanged?.        Invoke (this, new PropertyChangedEventArgs); }///<summary>//Configuration class///</summary>//<param name= "configuration" > Config &lt        ;/param> public configsetting (configuration configuration) {configuration = Configuration;        }///<summary>///Current configuration///</summary> Public Configuring configuration        {get;        } public event PropertyChangedEventHandler propertychanged;        <summary>///Get Current program configuration///</summary>//<param name= "config" ></param>            <returns></returns> public static Configuration getcurrentconfiguration () { Return Configurationmanager.openexeconfiguration (configuratioNuserlevel.none); }///<summary>//Returns the value of the specified configuration item///</summary>//<param name= "Settingkey" >&  lt;/param>//<returns></returns> protected virtual string Getsettingvalue (String Settingkey = null) {if (string. IsNullOrEmpty (settingkey)) Settingkey = new StackTrace (true). GetFrame (1). GetMethod ().                        Name.replace ("get_", "" "); Return Configuration?. AppSettings?. Settings[settingkey]?.        Value; }///<summary>///Returns the value of the specified configuration item///</summary>//<typeparam name= "T" > Value type &lt        ;/typeparam>//<param name= "Settingkey" ></param>///<returns></returns> Protected virtual T getsettingvalue<t> (string settingkey = null) {if (string. IsNullOrEmpty (settingkey)) Settingkey = new StackTrace (true). GetFrame (1). GetMethod (). Name.replace ("get_", "" ");            var value = Getsettingvalue (Settingkey); if (string.            Isnullorwhitespace (value)) {return default (T);            } else {return (t) convert.changetype (value, typeof (T)); }}///<summary>///Set values for the specified configuration items///</summary>//<param name= "value" & gt;</param>//<param name= "Settingkey" ></param> protected virtual void Setsettingvalue (o Bject value, string settingkey = null) {if (string. IsNullOrEmpty (settingkey)) Settingkey = new StackTrace (true). GetFrame (1). GetMethod ().            Name.replace ("set_", "" "); Configuration.addorupdateappsettingitem (Settingkey, value?.            ToString ());            Notifypropertychanged (Settingkey);        Configuration.save (); }///<summary>//Returns the value of the specified configuration item///</summary>//<param name= "Settingkey" >& Lt/param>//<returns></returns> public string Getsettingvaluebykey (string settingkey)        {return getsettingvalue (Settingkey); }///<summary>//Returns the value of the specified configuration item///</summary>//<param name= "Settingkey" >&        lt;/param>//<returns></returns> public T getsettingvaluebykey<t> (string settingkey)        {return getsettingvalue<t> (Settingkey); }///<summary>//Set values for the specified configuration item///</summary>//<param name= "value" ></p aram>//<param name= "Settingkey" ></param> public void Setsettingvaluebykey (string Settingke            Y, Object value) {Setsettingvalue (value, Settingkey);        Notifypropertychanged (Settingkey); }    }

The INotifyPropertyChanged interface is implemented so that the interface refreshes the display when the value of the configuration item changes when the interface binds the configuration item.

Look at this article is not understand, we need to look at the original text.

C # Object-oriented way to set, read application configuration

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.