ValueCollectionParameterReader.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //
  2. // System.Web.Services.Protocols.ValueCollectionParameterReader.cs
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. // Lluis Sanchez Gual ([email protected])
  7. //
  8. // Copyright (C) Tim Coleman, 2002
  9. //
  10. using System.Collections.Specialized;
  11. using System.Reflection;
  12. using System.Web;
  13. using System.Xml;
  14. namespace System.Web.Services.Protocols {
  15. public abstract class ValueCollectionParameterReader : MimeParameterReader {
  16. ParameterInfo[] parameters;
  17. #region Constructors
  18. protected ValueCollectionParameterReader ()
  19. {
  20. }
  21. #endregion // Constructors
  22. #region Methods
  23. public override object GetInitializer (LogicalMethodInfo methodInfo)
  24. {
  25. if (IsSupported (methodInfo)) return methodInfo.Parameters;
  26. else return null;
  27. }
  28. public override void Initialize (object o)
  29. {
  30. parameters = (ParameterInfo[]) o;
  31. }
  32. public static bool IsSupported (LogicalMethodInfo methodInfo)
  33. {
  34. foreach (ParameterInfo param in methodInfo.Parameters)
  35. if (!IsSupported (param)) return false;
  36. return true;
  37. }
  38. public static bool IsSupported (ParameterInfo paramInfo)
  39. {
  40. Type type = paramInfo.ParameterType;
  41. if (type.IsByRef || paramInfo.IsOut) return false;
  42. if (type.IsArray) return IsSupportedPrimitive (type.GetElementType());
  43. else return IsSupportedPrimitive (type);
  44. }
  45. internal static bool IsSupportedPrimitive (Type type)
  46. {
  47. return ( type.IsPrimitive ||
  48. type == typeof(string) ||
  49. type == typeof(DateTime) ||
  50. type == typeof(Guid) ||
  51. type == typeof(XmlQualifiedName) ||
  52. type == typeof(TimeSpan) ||
  53. type == typeof(byte[])
  54. );
  55. }
  56. protected object[] Read (NameValueCollection collection)
  57. {
  58. object[] res = new object [parameters.Length];
  59. for (int n=0; n<res.Length; n++)
  60. {
  61. string val = collection [parameters[n].Name];
  62. if (val == null) throw new InvalidOperationException ("Missing parameter: " + parameters[n].Name);
  63. try
  64. {
  65. res [n] = Convert.ChangeType (val, parameters[n].ParameterType);
  66. }
  67. catch (Exception ex)
  68. {
  69. string error = "Cannot convert " + val + " to " + parameters[n].ParameterType.FullName + "\n";
  70. error += "Parameter name: " + parameters[n].Name + " --> " + ex.Message;
  71. throw new InvalidOperationException (error);
  72. }
  73. }
  74. return res;
  75. }
  76. #endregion // Methods
  77. }
  78. }