RouteValueDictionary.cs 5.0 KB

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