Initial commit
[mono.git] / mcs / class / referencesource / System / compmod / system / diagnostics / BooleanSwitch.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="BooleanSwitch.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //------------------------------------------------------------------------------
6
7 /*
8  */
9
10 namespace System.Diagnostics {
11     using System.Diagnostics;
12     using System;
13     using System.Security;
14     using System.Security.Permissions;
15
16     /// <devdoc>
17     ///    <para>Provides a simple on/off switch that can be used to control debugging and tracing
18     ///       output.</para>
19     /// </devdoc>
20     [SwitchLevel(typeof(bool))]
21     public class BooleanSwitch : Switch {
22         /// <devdoc>
23         /// <para>Initializes a new instance of the <see cref='System.Diagnostics.BooleanSwitch'/>
24         /// class.</para>
25         /// </devdoc>
26         public BooleanSwitch(string displayName, string description)
27             : base(displayName, description) {
28         }
29
30         public BooleanSwitch(string displayName, string description, string defaultSwitchValue) 
31             : base(displayName, description, defaultSwitchValue) { }
32
33         /// <devdoc>
34         ///    <para>Specifies whether the switch is enabled
35         ///       (<see langword='true'/>) or disabled (<see langword='false'/>).</para>
36         /// </devdoc>
37         public bool Enabled {
38             get {
39                 return (SwitchSetting == 0) ? false : true;
40             }
41             [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
42             set {
43                 SwitchSetting = value ? 1 : 0;
44             }
45         }
46
47         protected override void OnValueChanged() {
48             bool b;
49             if (Boolean.TryParse(Value, out b))
50                 SwitchSetting = ( b ? 1 : 0);
51             else
52                 base.OnValueChanged();
53         }
54     }
55 }
56