QueryStringConverter.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // QueryStringConverter.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.ComponentModel;
  30. using System.Globalization;
  31. using System.ServiceModel;
  32. using System.ServiceModel.Description;
  33. namespace System.ServiceModel.Dispatcher
  34. {
  35. public class QueryStringConverter
  36. {
  37. // "Service Operation Parameters and URLs"
  38. // http://msdn2.microsoft.com/en-us/library/bb412172.aspx
  39. public virtual bool CanConvert (Type type)
  40. {
  41. switch (Type.GetTypeCode (type)) {
  42. case TypeCode.DBNull:
  43. case TypeCode.Empty:
  44. return false;
  45. case TypeCode.Object:
  46. if (type == typeof (TimeSpan))
  47. return true;
  48. if (type == typeof (DateTimeOffset))
  49. return true;
  50. if (type == typeof (Guid))
  51. return true;
  52. if (type == typeof (object))
  53. return true;
  54. // if (type.GetCustomAttributes (typeof (TypeConverterAttribute), true).Length > 0)
  55. // return true;
  56. return false;
  57. default:
  58. return true;
  59. }
  60. }
  61. public virtual object ConvertStringToValue (string parameter, Type parameterType)
  62. {
  63. if (parameterType == null)
  64. throw new ArgumentNullException ("parameterType");
  65. if (!CanConvert (parameterType))
  66. throw new NotSupportedException (String.Format ("Conversion from the argument parameterType '{0}' is not supported", parameterType));
  67. if (parameterType.IsEnum)
  68. return Enum.Parse(parameterType, parameter, true);
  69. switch (Type.GetTypeCode (parameterType)) {
  70. case TypeCode.String:
  71. return parameter;
  72. case TypeCode.Char:
  73. return parameter != null ? Char.Parse (parameter) : default (char);
  74. case TypeCode.SByte:
  75. return parameter != null ? SByte.Parse (parameter, CultureInfo.InvariantCulture): default (sbyte);
  76. case TypeCode.Byte:
  77. return parameter != null ? Byte.Parse (parameter, CultureInfo.InvariantCulture): default (byte);
  78. case TypeCode.Int16:
  79. return parameter != null ? Int16.Parse (parameter, CultureInfo.InvariantCulture): default (short);
  80. case TypeCode.Int32:
  81. return parameter != null ? Int32.Parse (parameter, CultureInfo.InvariantCulture): default (int);
  82. case TypeCode.Int64:
  83. return parameter != null ? Int64.Parse (parameter, CultureInfo.InvariantCulture): default (long);
  84. case TypeCode.UInt16:
  85. return parameter != null ? UInt16.Parse (parameter, CultureInfo.InvariantCulture): default (ushort);
  86. case TypeCode.UInt32:
  87. return parameter != null ? UInt32.Parse (parameter, CultureInfo.InvariantCulture): default (uint);
  88. case TypeCode.UInt64:
  89. return parameter != null ? UInt64.Parse (parameter, CultureInfo.InvariantCulture): default (ulong);
  90. case TypeCode.DateTime:
  91. return parameter != null ? DateTime.Parse (parameter, CultureInfo.InvariantCulture): default (DateTime);
  92. case TypeCode.Boolean:
  93. return parameter != null ? Boolean.Parse (parameter): default (bool);
  94. case TypeCode.Single:
  95. return parameter != null ? Single.Parse (parameter, CultureInfo.InvariantCulture): default (float);
  96. case TypeCode.Double:
  97. return parameter != null ? Double.Parse (parameter, CultureInfo.InvariantCulture): default (double);
  98. case TypeCode.Decimal:
  99. return parameter != null ? Decimal.Parse (parameter, CultureInfo.InvariantCulture): default (decimal);
  100. case TypeCode.Object:
  101. if (parameterType == typeof (TimeSpan))
  102. return TimeSpan.Parse (parameter);
  103. if (parameterType == typeof (DateTimeOffset))
  104. return DateTimeOffset.Parse (parameter, CultureInfo.InvariantCulture);
  105. if (parameterType == typeof (Guid))
  106. return new Guid (parameter);
  107. break;
  108. }
  109. throw new NotSupportedException (String.Format ("Cannot convert parameter string '{0}' to parameter type '{1}'", parameter, parameterType));
  110. }
  111. public virtual string ConvertValueToString (object parameter, Type parameterType)
  112. {
  113. if (parameterType == null)
  114. throw new ArgumentNullException ("parameterType");
  115. if (parameterType.IsValueType && parameter == null)
  116. throw new ArgumentNullException ("parameter");
  117. if (parameter == null)
  118. return null;
  119. if (parameter.GetType () != parameterType)
  120. throw new InvalidCastException (String.Format ("This QueryStringConverter does not support cast from {0} to {1}", parameter.GetType (), parameterType));
  121. if (!CanConvert (parameterType))
  122. throw new NotSupportedException (String.Format ("Conversion from the argument parameterType '{0}' is not supported", parameterType));
  123. if (parameter is IFormattable)
  124. ((IFormattable) parameter).ToString (null, CultureInfo.InvariantCulture);
  125. return parameter.ToString ();
  126. }
  127. }
  128. }