Switch.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. using System.Collections;
  14. namespace System.Diagnostics
  15. {
  16. public abstract class Switch
  17. {
  18. private string name = "";
  19. private string description = "";
  20. private int switchSetting = 0;
  21. // MS Behavior is that (quoting from MSDN for OnSwitchSettingChanged()):
  22. // "...It is invoked the first time a switch reads its value from the
  23. // configuration file..."
  24. // The docs + testing implies two things:
  25. // 1. The value of the switch is not read in from the constructor
  26. // 2. The value is instead read in on the first time get_SwitchSetting is
  27. // invoked
  28. // Assuming that OnSwitchSettingChanged() is invoked on a .config file
  29. // read and on all changes
  30. //
  31. // Thus, we need to keep track of whether or not switchSetting has been
  32. // initialized. Using `switchSetting=-1' seems logical, but if someone
  33. // actually wants to use -1 as a switch value that would cause problems.
  34. private bool initialized = false;
  35. protected Switch(string displayName, string description)
  36. {
  37. this.name = displayName;
  38. this.description = description;
  39. }
  40. public string Description {
  41. get {return description;}
  42. }
  43. public string DisplayName {
  44. get {return name;}
  45. }
  46. protected int SwitchSetting {
  47. get {
  48. if (!initialized) {
  49. initialized = true;
  50. GetConfigFileSetting ();
  51. OnSwitchSettingChanged ();
  52. }
  53. return switchSetting;
  54. }
  55. set {
  56. if(switchSetting != value) {
  57. switchSetting = value;
  58. OnSwitchSettingChanged();
  59. }
  60. initialized = true;
  61. }
  62. }
  63. private void GetConfigFileSetting ()
  64. {
  65. // Load up the specified switch
  66. IDictionary d =
  67. (IDictionary) DiagnosticsConfiguration.Settings ["switches"];
  68. if (d != null) {
  69. object o = d [name];
  70. try {
  71. switchSetting = int.Parse (o.ToString());
  72. }
  73. catch {
  74. switchSetting = 0;
  75. }
  76. }
  77. }
  78. protected virtual void OnSwitchSettingChanged()
  79. {
  80. // Do nothing. This is merely provided for derived classes to know when
  81. // the value of SwitchSetting has changed.
  82. }
  83. }
  84. }