Switch.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // System.Diagnostics.Switch.cs
  3. //
  4. // Comments from John R. Hicks <[email protected]> original implementation
  5. // can be found at: /mcs/docs/apidocs/xml/en/System.Diagnostics
  6. //
  7. // Author:
  8. // John R. Hicks ([email protected])
  9. // Jonathan Pryor ([email protected])
  10. //
  11. // (C) 2001-2002
  12. //
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System.Collections;
  34. namespace System.Diagnostics
  35. {
  36. public abstract class Switch
  37. {
  38. private string name = "";
  39. private string description = "";
  40. private int switchSetting = 0;
  41. // MS Behavior is that (quoting from MSDN for OnSwitchSettingChanged()):
  42. // "...It is invoked the first time a switch reads its value from the
  43. // configuration file..."
  44. // The docs + testing implies two things:
  45. // 1. The value of the switch is not read in from the constructor
  46. // 2. The value is instead read in on the first time get_SwitchSetting is
  47. // invoked
  48. // Assuming that OnSwitchSettingChanged() is invoked on a .config file
  49. // read and on all changes
  50. //
  51. // Thus, we need to keep track of whether or not switchSetting has been
  52. // initialized. Using `switchSetting=-1' seems logical, but if someone
  53. // actually wants to use -1 as a switch value that would cause problems.
  54. private bool initialized = false;
  55. protected Switch(string displayName, string description)
  56. {
  57. this.name = displayName;
  58. this.description = description;
  59. }
  60. public string Description {
  61. get {return description;}
  62. }
  63. public string DisplayName {
  64. get {return name;}
  65. }
  66. protected int SwitchSetting {
  67. get {
  68. if (!initialized) {
  69. initialized = true;
  70. GetConfigFileSetting ();
  71. OnSwitchSettingChanged ();
  72. }
  73. return switchSetting;
  74. }
  75. set {
  76. if(switchSetting != value) {
  77. switchSetting = value;
  78. OnSwitchSettingChanged();
  79. }
  80. initialized = true;
  81. }
  82. }
  83. private void GetConfigFileSetting ()
  84. {
  85. try {
  86. // Load up the specified switch
  87. IDictionary d = (IDictionary) DiagnosticsConfiguration.Settings ["switches"];
  88. if (d != null)
  89. switchSetting = int.Parse (d [name].ToString());
  90. } catch {
  91. switchSetting = 0;
  92. }
  93. }
  94. protected virtual void OnSwitchSettingChanged()
  95. {
  96. // Do nothing. This is merely provided for derived classes to know when
  97. // the value of SwitchSetting has changed.
  98. }
  99. }
  100. }