MyHttpRequestWrapper.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Web;
  8. namespace MonoTests.Common
  9. {
  10. class MyHttpRequestWrapper : HttpRequestBase
  11. {
  12. Dictionary<string, object> propertyValues = new Dictionary<string, object> ();
  13. public override string AppRelativeCurrentExecutionFilePath
  14. {
  15. get
  16. {
  17. string value;
  18. if (!GetProperty<string> ("AppRelativeCurrentExecutionFilePath", out value))
  19. return base.AppRelativeCurrentExecutionFilePath;
  20. return value;
  21. }
  22. }
  23. public override string PathInfo
  24. {
  25. get
  26. {
  27. string value;
  28. if (!GetProperty<string> ("PathInfo", out value))
  29. return base.PathInfo;
  30. return value;
  31. }
  32. }
  33. public override NameValueCollection QueryString
  34. {
  35. get
  36. {
  37. NameValueCollection value;
  38. if (!GetProperty<NameValueCollection> ("QueryString", out value))
  39. return base.QueryString;
  40. return value;
  41. }
  42. }
  43. bool GetProperty<T> (string name, out T value)
  44. {
  45. if (String.IsNullOrEmpty (name))
  46. throw new ArgumentNullException ("name");
  47. value = default (T);
  48. object v;
  49. if (propertyValues.TryGetValue (name, out v)) {
  50. if (v == null)
  51. return true;
  52. if (typeof (T).IsAssignableFrom (v.GetType ())) {
  53. value = (T) v;
  54. return true;
  55. }
  56. throw new InvalidOperationException ("Invalid value type. Expected '" + typeof (T) + "' and got '" + v.GetType () + "'");
  57. }
  58. return false;
  59. }
  60. public void SetProperty (string name, object value)
  61. {
  62. if (String.IsNullOrEmpty (name))
  63. return;
  64. if (propertyValues.ContainsKey (name))
  65. propertyValues[name] = value;
  66. else
  67. propertyValues.Add (name, value);
  68. }
  69. }
  70. }