Merge pull request #1936 from esdrubal/DotNetRelativeOrAbsolute
[mono.git] / mcs / class / System.Web / System.Web.Routing / RouteValueDictionary.cs
1 //
2 // RouteValueDictionary.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2008 Novell Inc. http://novell.com
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 using System;
31 using System.Collections;
32 using System.Collections.Generic;
33 using System.Globalization;
34 using System.Runtime.CompilerServices;
35 using System.Security.Permissions;
36 using System.Web;
37
38 using PairCollection = System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, object>>;
39
40 namespace System.Web.Routing
41 {
42         [TypeForwardedFrom ("System.Web.Routing, Version=3.5.0.0, Culture=Neutral, PublicKeyToken=31bf3856ad364e35")]
43         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
44         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
45         public class RouteValueDictionary : IDictionary<string, object>
46         {
47                 internal class CaseInsensitiveStringComparer : IEqualityComparer<string>
48                 {
49                         public static readonly CaseInsensitiveStringComparer Instance = new CaseInsensitiveStringComparer ();
50
51                         public int GetHashCode (string obj)
52                         {
53                                 return obj.ToLower (CultureInfo.InvariantCulture).GetHashCode ();
54                         }
55
56                         public bool Equals (string obj1, string obj2)
57                         {
58                                 return String.Equals (obj1, obj2, StringComparison.OrdinalIgnoreCase);
59                         }
60                 }
61
62                 Dictionary<string,object> d = new Dictionary<string,object> (CaseInsensitiveStringComparer.Instance);
63
64                 public RouteValueDictionary ()
65                 {
66                 }
67
68                 public RouteValueDictionary (IDictionary<string, object> dictionary)
69                 {
70                         if (dictionary == null)
71                                 throw new ArgumentNullException ("dictionary");
72                         foreach (var p in dictionary)
73                                 Add (p.Key, p.Value);
74                 }
75
76                 public RouteValueDictionary (object values) // anonymous type instance
77                 {
78                         if (values == null)
79                                 return;
80
81                         foreach (var pi in values.GetType ().GetProperties ()) {
82                                 try {
83                                         Add (pi.Name, pi.GetValue (values, null));
84                                 } catch {
85                                         // ignore
86                                 }
87                         }
88                 }
89
90                 public int Count {
91                         get { return d.Count; }
92                 }
93
94                 bool PairCollection.IsReadOnly {
95                         get { return ((PairCollection) d).IsReadOnly; }
96                 }
97
98                 ICollection<string> IDictionary<string, object>.Keys {
99                         get { return d.Keys; }
100                 }
101
102                 ICollection<Object> IDictionary<string, object>.Values {
103                         get { return d.Values; }
104                 }
105
106                 public object this [string key] {
107                         get { object v; return d.TryGetValue (key, out v) ? v : null; }
108                         set { d [key] = value; }
109                 }
110
111                 public Dictionary<string, object>.KeyCollection Keys {
112                         get { return d.Keys; }
113                 }
114
115                 public Dictionary<string, object>.ValueCollection Values {
116                         get { return d.Values; }
117                 }
118
119                 public void Add (string key, object value)
120                 {
121                         d.Add (key, value);
122                 }
123
124                 public void Clear ()
125                 {
126                         d.Clear ();
127                 }
128
129                 public bool ContainsKey (string key)
130                 {
131                         return d.ContainsKey (key);
132                 }
133
134                 public bool ContainsValue (object value)
135                 {
136                         return d.ContainsValue (value);
137                 }
138
139                 public Dictionary<string, object>.Enumerator GetEnumerator ()
140                 {
141                         return d.GetEnumerator ();
142                 }
143
144                 void ICollection<KeyValuePair<string, object>>.Add (KeyValuePair<string, object> item)
145                 {
146                         ((PairCollection) d).Add (item);
147                 }
148
149                 bool ICollection<KeyValuePair<string, object>>.Contains (KeyValuePair<string, object> item)
150                 {
151                         return ((PairCollection) d).Contains (item);
152                 }
153
154                 void ICollection<KeyValuePair<string, object>>.CopyTo (KeyValuePair<string, object> [] array, int arrayIndex)
155                 {
156                         ((PairCollection) d).CopyTo (array, arrayIndex);
157                 }
158
159                 bool ICollection<KeyValuePair<string, object>>.Remove (KeyValuePair<string, object> item)
160                 {
161                         return ((PairCollection) d).Remove (item);
162                 }
163
164                 IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator()
165                 {
166                         return d.GetEnumerator ();
167                 }
168
169                 IEnumerator IEnumerable.GetEnumerator ()
170                 {
171                         return d.GetEnumerator ();
172                 }
173
174                 public bool Remove (string key)
175                 {
176                         return d.Remove (key);
177                 }
178
179                 public bool TryGetValue (string key, out object value)
180                 {
181                         return d.TryGetValue (key, out value);
182                 }
183         }
184 }