ValueCollectionParameterReader.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. //
  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.
  29. //
  30. using System.Collections.Specialized;
  31. using System.Reflection;
  32. using System.Web;
  33. using System.Xml;
  34. namespace System.Web.Services.Protocols {
  35. public abstract class ValueCollectionParameterReader : MimeParameterReader {
  36. ParameterInfo[] parameters;
  37. #region Constructors
  38. protected ValueCollectionParameterReader ()
  39. {
  40. }
  41. #endregion // Constructors
  42. #region Methods
  43. public override object GetInitializer (LogicalMethodInfo methodInfo)
  44. {
  45. if (IsSupported (methodInfo)) return methodInfo.Parameters;
  46. else return null;
  47. }
  48. public override void Initialize (object o)
  49. {
  50. parameters = (ParameterInfo[]) o;
  51. }
  52. public static bool IsSupported (LogicalMethodInfo methodInfo)
  53. {
  54. foreach (ParameterInfo param in methodInfo.Parameters)
  55. if (!IsSupported (param)) return false;
  56. return true;
  57. }
  58. public static bool IsSupported (ParameterInfo paramInfo)
  59. {
  60. Type type = paramInfo.ParameterType;
  61. if (type.IsByRef || paramInfo.IsOut) return false;
  62. if (type.IsArray) return IsSupportedPrimitive (type.GetElementType());
  63. else return IsSupportedPrimitive (type);
  64. }
  65. internal static bool IsSupportedPrimitive (Type type)
  66. {
  67. return ( type.IsPrimitive ||
  68. type == typeof(string) ||
  69. type == typeof(DateTime) ||
  70. type == typeof(Guid) ||
  71. type == typeof(XmlQualifiedName) ||
  72. type == typeof(TimeSpan) ||
  73. type == typeof(byte[])
  74. );
  75. }
  76. protected object[] Read (NameValueCollection collection)
  77. {
  78. object[] res = new object [parameters.Length];
  79. for (int n=0; n<res.Length; n++)
  80. {
  81. string val = collection [parameters[n].Name];
  82. if (val == null) throw new InvalidOperationException ("Missing parameter: " + parameters[n].Name);
  83. try
  84. {
  85. res [n] = Convert.ChangeType (val, parameters[n].ParameterType);
  86. }
  87. catch (Exception ex)
  88. {
  89. string error = "Cannot convert " + val + " to " + parameters[n].ParameterType.FullName + "\n";
  90. error += "Parameter name: " + parameters[n].Name + " --> " + ex.Message;
  91. throw new InvalidOperationException (error);
  92. }
  93. }
  94. return res;
  95. }
  96. #endregion // Methods
  97. }
  98. }