2 // System.Diagnostics.Switch.cs
4 // Comments from John R. Hicks <angryjohn69@nc.rr.com> original implementation
5 // can be found at: /mcs/docs/apidocs/xml/en/System.Diagnostics
8 // John R. Hicks (angryjohn69@nc.rr.com)
9 // Jonathan Pryor (jonpryor@vt.edu)
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Collections;
36 using System.Collections.Specialized;
38 using System.Configuration;
41 namespace System.Diagnostics
43 public abstract class Switch
46 private string description;
47 private int switchSetting;
49 private string defaultSwitchValue;
50 // MS Behavior is that (quoting from MSDN for OnSwitchSettingChanged()):
51 // "...It is invoked the first time a switch reads its value from the
52 // configuration file..."
53 // The docs + testing implies two things:
54 // 1. The value of the switch is not read in from the constructor
55 // 2. The value is instead read in on the first time get_SwitchSetting is
57 // Assuming that OnSwitchSettingChanged() is invoked on a .config file
58 // read and on all changes
60 // Thus, we need to keep track of whether or not switchSetting has been
61 // initialized. Using `switchSetting=-1' seems logical, but if someone
62 // actually wants to use -1 as a switch value that would cause problems.
63 private bool initialized;
65 protected Switch(string displayName, string description)
67 this.name = displayName;
68 this.description = description;
71 protected Switch(string displayName, string description, string defaultSwitchValue)
72 : this (displayName, description)
74 this.defaultSwitchValue = defaultSwitchValue;
77 public string Description {
78 get {return description;}
81 public string DisplayName {
85 protected int SwitchSetting {
89 GetConfigFileSetting ();
90 OnSwitchSettingChanged ();
95 if(switchSetting != value) {
96 switchSetting = value;
97 OnSwitchSettingChanged();
103 StringDictionary attributes = new StringDictionary ();
106 [System.Xml.Serialization.XmlIgnore]
108 public StringDictionary Attributes {
109 get { return attributes; }
112 protected string Value {
113 get { return value; }
116 #if CONFIGURATION_DEP
119 } catch (Exception ex) {
120 string msg = string.Format ("The config "
121 + "value for Switch '{0}' was "
122 + "invalid.", DisplayName);
124 throw new ConfigurationErrorsException (
133 protected internal virtual string [] GetSupportedAttributes ()
138 protected virtual void OnValueChanged ()
142 private void GetConfigFileSetting ()
144 IDictionary d = (IDictionary) DiagnosticsConfiguration.Settings ["switches"];
146 // Load up the specified switch
148 if (d.Contains (name)) {
149 #if CONFIGURATION_DEP
150 Value = d [name] as string;
152 switchSetting = (int) d [name];
158 if (defaultSwitchValue != null) {
159 value = defaultSwitchValue;
164 protected virtual void OnSwitchSettingChanged()
166 // Do nothing. This is merely provided for derived classes to know when
167 // the value of SwitchSetting has changed.