Merge pull request #347 from JamesB7/master
[mono.git] / mcs / class / System / System.Configuration / SettingElement.cs
1 //
2 // System.Web.UI.WebControls.SettingElement.cs
3 //
4 // Authors:
5 //      Chris Toshok (toshok@ximian.com)
6 //
7 // (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30
31 namespace System.Configuration
32 {
33         public sealed class SettingElement
34 #if (CONFIGURATION_DEP)
35                 : ConfigurationElement
36 #endif
37         {
38 #if CONFIGURATION_DEP
39                 static ConfigurationPropertyCollection properties;
40                 static ConfigurationProperty name_prop, serialize_as_prop, value_prop;
41 #endif
42
43                 static SettingElement ()
44                 {
45 #if CONFIGURATION_DEP
46                         name_prop = new ConfigurationProperty ("name", typeof (string), String.Empty, ConfigurationPropertyOptions.IsRequired | ConfigurationPropertyOptions.IsKey);
47                         serialize_as_prop = new ConfigurationProperty ("serializeAs", typeof (SettingsSerializeAs), null, ConfigurationPropertyOptions.IsRequired);
48                         value_prop = new ConfigurationProperty ("value", typeof (SettingValueElement), null, ConfigurationPropertyOptions.IsRequired);
49                         properties = new ConfigurationPropertyCollection ();
50
51                         properties.Add (name_prop);
52                         properties.Add (serialize_as_prop);
53                         properties.Add (value_prop);
54 #endif
55                 }
56
57                 public SettingElement ()
58                 {
59                 }
60
61                 public SettingElement (string name,
62                                        SettingsSerializeAs serializeAs)
63                 {
64 #if CONFIGURATION_DEP
65                         Name = name;
66                         SerializeAs = serializeAs;
67 #endif
68                 }
69
70 #if (CONFIGURATION_DEP)
71                 [ConfigurationProperty ("name", DefaultValue="",
72                                         Options = ConfigurationPropertyOptions.IsRequired | ConfigurationPropertyOptions.IsKey)]
73                 public string Name {
74                         get { return (string) base [name_prop]; }
75                         set { base [name_prop] = value; } // it does not reject null
76                 }
77
78                 [ConfigurationProperty ("value", DefaultValue=null,
79                                         Options = ConfigurationPropertyOptions.IsRequired)]
80                 public SettingValueElement Value {
81                         get { return (SettingValueElement) base [value_prop]; }
82                         set { base [value_prop] = value; }
83                 }
84
85                 [ConfigurationProperty ("serializeAs", DefaultValue=SettingsSerializeAs.String,
86                                         Options = ConfigurationPropertyOptions.IsRequired)]
87                 public SettingsSerializeAs SerializeAs {
88                         get { return base [serialize_as_prop] != null ? (SettingsSerializeAs) base [serialize_as_prop] : default (SettingsSerializeAs); }
89                         set { base [serialize_as_prop] = value; }
90                 }
91
92                 protected override ConfigurationPropertyCollection Properties {
93                         get { return properties; }
94                 }
95
96                 public override bool Equals (object o)
97                 {
98                         SettingElement e = o as SettingElement;
99                         if (e == null)
100                                 return false;
101
102                         return e.SerializeAs == SerializeAs && e.Value == Value && e.Name == Name;
103                 }
104
105                 public override int GetHashCode ()
106                 {
107                         int v = (int) SerializeAs ^ 0x7F;
108                         if (Name != null)
109                                 v += Name.GetHashCode () ^ 0x7F;
110                         if (Value != null)
111                                 v += Value.GetHashCode ();
112                         return v;
113                 }
114 #endif
115         }
116
117 }
118