The Labs \ Source Viewer \ SSCLI \ System.Diagnostics \ BooleanSwitch

  1. //------------------------------------------------------------------------------
  2. // <copyright file="BooleanSwitch.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. /*
  16. */
  17. namespace System.Diagnostics
  18. {
  19.     using System.Diagnostics;
  20.     using System;
  21.     using System.Security;
  22.     using System.Security.Permissions;
  23.    
  24.     /// <devdoc>
  25.     /// <para>Provides a simple on/off switch that can be used to control debugging and tracing
  26.     /// output.</para>
  27.     /// </devdoc>
  28.     [SwitchLevel(typeof(bool))]
  29.     public class BooleanSwitch : Switch
  30.     {
  31.         /// <devdoc>
  32.         /// <para>Initializes a new instance of the <see cref='System.Diagnostics.BooleanSwitch'/>
  33.         /// class.</para>
  34.         /// </devdoc>
  35.         public BooleanSwitch(string displayName, string description) : base(displayName, description)
  36.         {
  37.         }
  38.        
  39.         public BooleanSwitch(string displayName, string description, string defaultSwitchValue) : base(displayName, description, defaultSwitchValue)
  40.         {
  41.         }
  42.        
  43.         /// <devdoc>
  44.         /// <para>Specifies whether the switch is enabled
  45.         /// (<see langword='true'/>) or disabled (<see langword='false'/>).</para>
  46.         /// </devdoc>
  47.         public bool Enabled {
  48.             get { return (SwitchSetting == 0) ? false : true; }
  49.             [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
  50.             set { SwitchSetting = value ? 1 : 0; }
  51.         }
  52.        
  53.         protected override void OnValueChanged()
  54.         {
  55.             bool b;
  56.             if (Boolean.TryParse(Value, out b))
  57.                 SwitchSetting = (b ? 1 : 0);
  58.             else
  59.                 base.OnValueChanged();
  60.         }
  61.     }
  62. }

Developer Fusion