Load custom attribute data using Cecil if possible in TypeMirror/FieldInfoMirror...
[mono.git] / mcs / class / System.Web.Routing / 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 #if NET_4_0
43         [TypeForwardedFrom ("System.Web.Routing, Version=3.5.0.0, Culture=Neutral, PublicKeyToken=31bf3856ad364e35")]
44 #endif
45         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
46         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
47         public class RouteValueDictionary : IDictionary<string, object>
48         {
49                 internal class CaseInsensitiveStringComparer : IEqualityComparer<string>
50                 {
51                         public static readonly CaseInsensitiveStringComparer Instance = new CaseInsensitiveStringComparer ();
52
53                         public int GetHashCode (string obj)
54                         {
55                                 return obj.ToLower (CultureInfo.InvariantCulture).GetHashCode ();
56                         }
57
58                         public bool Equals (string obj1, string obj2)
59                         {
60                                 return String.Equals (obj1, obj2, StringComparison.OrdinalIgnoreCase);
61                         }
62                 }
63
64                 Dictionary<string,object> d = new Dictionary<string,object> (CaseInsensitiveStringComparer.Instance);
65
66                 public RouteValueDictionary ()
67                 {
68                 }
69
70                 public RouteValueDictionary (IDictionary<string, object> dictionary)
71                 {
72                         if (dictionary == null)
73                                 throw new ArgumentNullException ("dictionary");
74                         foreach (var p in dictionary)
75                                 Add (p.Key, p.Value);
76                 }
77
78                 public RouteValueDictionary (object values) // anonymous type instance
79                 {
80                         if (values == null)
81                                 return;
82
83                         foreach (var pi in values.GetType ().GetProperties ()) {
84                                 try {
85                                         Add (pi.Name, pi.GetValue (values, null));
86                                 } catch {
87                                         // ignore
88                                 }
89                         }
90                 }
91
92                 public int Count {
93                         get { return d.Count; }
94                 }
95
96                 bool PairCollection.IsReadOnly {
97                         get { return ((PairCollection) d).IsReadOnly; }
98                 }
99
100                 ICollection<string> IDictionary<string, object>.Keys {
101                         get { return d.Keys; }
102                 }
103
104                 ICollection<Object> IDictionary<string, object>.Values {
105                         get { return d.Values; }
106                 }
107
108                 public object this [string key] {
109                         get { object v; return d.TryGetValue (key, out v) ? v : null; }
110                         set { d [key] = value; }
111                 }
112
113                 public Dictionary<string, object>.KeyCollection Keys {
114                         get { return d.Keys; }
115                 }
116
117                 public Dictionary<string, object>.ValueCollection Values {
118                         get { return d.Values; }
119                 }
120
121                 public void Add (string key, object value)
122                 {
123                         d.Add (key, value);
124                 }
125
126                 public void Clear ()
127                 {
128                         d.Clear ();
129                 }
130
131                 public bool ContainsKey (string key)
132                 {
133                         return d.ContainsKey (key);
134                 }
135
136                 public bool ContainsValue (object value)
137                 {
138                         return d.ContainsValue (value);
139                 }
140
141                 public Dictionary<string, object>.Enumerator GetEnumerator ()
142                 {
143                         return d.GetEnumerator ();
144                 }
145
146                 void ICollection<KeyValuePair<string, object>>.Add (KeyValuePair<string, object> item)
147                 {
148                         ((PairCollection) d).Add (item);
149                 }
150
151                 bool ICollection<KeyValuePair<string, object>>.Contains (KeyValuePair<string, object> item)
152                 {
153                         return ((PairCollection) d).Contains (item);
154                 }
155
156                 void ICollection<KeyValuePair<string, object>>.CopyTo (KeyValuePair<string, object> [] array, int arrayIndex)
157                 {
158                         ((PairCollection) d).CopyTo (array, arrayIndex);
159                 }
160
161                 bool ICollection<KeyValuePair<string, object>>.Remove (KeyValuePair<string, object> item)
162                 {
163                         return ((PairCollection) d).Remove (item);
164                 }
165
166                 IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator()
167                 {
168                         return d.GetEnumerator ();
169                 }
170
171                 IEnumerator IEnumerable.GetEnumerator ()
172                 {
173                         return d.GetEnumerator ();
174                 }
175
176                 public bool Remove (string key)
177                 {
178                         return d.Remove (key);
179                 }
180
181                 public bool TryGetValue (string key, out object value)
182                 {
183                         return d.TryGetValue (key, out value);
184                 }
185         }
186 }