2 // RouteValueDictionary.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2008 Novell Inc. http://novell.com
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
31 using System.Collections;
32 using System.Collections.Generic;
33 using System.Globalization;
34 using System.Runtime.CompilerServices;
35 using System.Security.Permissions;
38 using PairCollection = System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, object>>;
40 namespace System.Web.Routing
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>
47 internal class CaseInsensitiveStringComparer : IEqualityComparer<string>
49 public static readonly CaseInsensitiveStringComparer Instance = new CaseInsensitiveStringComparer ();
51 public int GetHashCode (string obj)
53 return obj.ToLower (CultureInfo.InvariantCulture).GetHashCode ();
56 public bool Equals (string obj1, string obj2)
58 return String.Equals (obj1, obj2, StringComparison.OrdinalIgnoreCase);
62 Dictionary<string,object> d = new Dictionary<string,object> (CaseInsensitiveStringComparer.Instance);
64 public RouteValueDictionary ()
68 public RouteValueDictionary (IDictionary<string, object> dictionary)
70 if (dictionary == null)
71 throw new ArgumentNullException ("dictionary");
72 foreach (var p in dictionary)
76 public RouteValueDictionary (object values) // anonymous type instance
81 foreach (var pi in values.GetType ().GetProperties ()) {
83 Add (pi.Name, pi.GetValue (values, null));
91 get { return d.Count; }
94 bool PairCollection.IsReadOnly {
95 get { return ((PairCollection) d).IsReadOnly; }
98 ICollection<string> IDictionary<string, object>.Keys {
99 get { return d.Keys; }
102 ICollection<Object> IDictionary<string, object>.Values {
103 get { return d.Values; }
106 public object this [string key] {
107 get { object v; return d.TryGetValue (key, out v) ? v : null; }
108 set { d [key] = value; }
111 public Dictionary<string, object>.KeyCollection Keys {
112 get { return d.Keys; }
115 public Dictionary<string, object>.ValueCollection Values {
116 get { return d.Values; }
119 public void Add (string key, object value)
129 public bool ContainsKey (string key)
131 return d.ContainsKey (key);
134 public bool ContainsValue (object value)
136 return d.ContainsValue (value);
139 public Dictionary<string, object>.Enumerator GetEnumerator ()
141 return d.GetEnumerator ();
144 void ICollection<KeyValuePair<string, object>>.Add (KeyValuePair<string, object> item)
146 ((PairCollection) d).Add (item);
149 bool ICollection<KeyValuePair<string, object>>.Contains (KeyValuePair<string, object> item)
151 return ((PairCollection) d).Contains (item);
154 void ICollection<KeyValuePair<string, object>>.CopyTo (KeyValuePair<string, object> [] array, int arrayIndex)
156 ((PairCollection) d).CopyTo (array, arrayIndex);
159 bool ICollection<KeyValuePair<string, object>>.Remove (KeyValuePair<string, object> item)
161 return ((PairCollection) d).Remove (item);
164 IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator()
166 return d.GetEnumerator ();
169 IEnumerator IEnumerable.GetEnumerator ()
171 return d.GetEnumerator ();
174 public bool Remove (string key)
176 return d.Remove (key);
179 public bool TryGetValue (string key, out object value)
181 return d.TryGetValue (key, out value);