add ISafeSerializationData
[mcs.git] / class / System.Web.Routing / System.Web.Routing / RouteValueDictionary.cs
blob7bd0b01ca01f9298b0e7f2d0be48d9e2c4bf2d89
1 //
2 // RouteValueDictionary.cs
3 //
4 // Author:
5 // Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2008 Novell Inc. http://novell.com
8 //
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.
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;
38 using PairCollection = System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, object>>;
40 namespace System.Web.Routing
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>
49 internal class CaseInsensitiveStringComparer : IEqualityComparer<string>
51 public static readonly CaseInsensitiveStringComparer Instance = new CaseInsensitiveStringComparer ();
53 public int GetHashCode (string obj)
55 return obj.ToLower (CultureInfo.InvariantCulture).GetHashCode ();
58 public bool Equals (string obj1, string obj2)
60 return String.Equals (obj1, obj2, StringComparison.OrdinalIgnoreCase);
64 Dictionary<string,object> d = new Dictionary<string,object> (CaseInsensitiveStringComparer.Instance);
66 public RouteValueDictionary ()
70 public RouteValueDictionary (IDictionary<string, object> dictionary)
72 if (dictionary == null)
73 throw new ArgumentNullException ("dictionary");
74 foreach (var p in dictionary)
75 Add (p.Key, p.Value);
78 public RouteValueDictionary (object values) // anonymous type instance
80 if (values == null)
81 return;
83 foreach (var pi in values.GetType ().GetProperties ()) {
84 try {
85 Add (pi.Name, pi.GetValue (values, null));
86 } catch {
87 // ignore
92 public int Count {
93 get { return d.Count; }
96 bool PairCollection.IsReadOnly {
97 get { return ((PairCollection) d).IsReadOnly; }
100 ICollection<string> IDictionary<string, object>.Keys {
101 get { return d.Keys; }
104 ICollection<Object> IDictionary<string, object>.Values {
105 get { return d.Values; }
108 public object this [string key] {
109 get { object v; return d.TryGetValue (key, out v) ? v : null; }
110 set { d [key] = value; }
113 public Dictionary<string, object>.KeyCollection Keys {
114 get { return d.Keys; }
117 public Dictionary<string, object>.ValueCollection Values {
118 get { return d.Values; }
121 public void Add (string key, object value)
123 d.Add (key, value);
126 public void Clear ()
128 d.Clear ();
131 public bool ContainsKey (string key)
133 return d.ContainsKey (key);
136 public bool ContainsValue (object value)
138 return d.ContainsValue (value);
141 public Dictionary<string, object>.Enumerator GetEnumerator ()
143 return d.GetEnumerator ();
146 void ICollection<KeyValuePair<string, object>>.Add (KeyValuePair<string, object> item)
148 ((PairCollection) d).Add (item);
151 bool ICollection<KeyValuePair<string, object>>.Contains (KeyValuePair<string, object> item)
153 return ((PairCollection) d).Contains (item);
156 void ICollection<KeyValuePair<string, object>>.CopyTo (KeyValuePair<string, object> [] array, int arrayIndex)
158 ((PairCollection) d).CopyTo (array, arrayIndex);
161 bool ICollection<KeyValuePair<string, object>>.Remove (KeyValuePair<string, object> item)
163 return ((PairCollection) d).Remove (item);
166 IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator()
168 return d.GetEnumerator ();
171 IEnumerator IEnumerable.GetEnumerator ()
173 return d.GetEnumerator ();
176 public bool Remove (string key)
178 return d.Remove (key);
181 public bool TryGetValue (string key, out object value)
183 return d.TryGetValue (key, out value);