2 // QueryStringConverter.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.ComponentModel;
30 using System.Globalization;
31 using System.ServiceModel;
32 using System.ServiceModel.Description;
34 namespace System.ServiceModel.Dispatcher
36 public class QueryStringConverter
38 // "Service Operation Parameters and URLs"
39 // http://msdn2.microsoft.com/en-us/library/bb412172.aspx
40 public virtual bool CanConvert (Type type)
42 switch (Type.GetTypeCode (type)) {
47 if (type == typeof (TimeSpan))
49 if (type == typeof (DateTimeOffset))
51 if (type == typeof (Guid))
53 if (type == typeof (object))
55 // if (type.GetCustomAttributes (typeof (TypeConverterAttribute), true).Length > 0)
63 public virtual object ConvertStringToValue (string parameter, Type parameterType)
65 if (parameterType == null)
66 throw new ArgumentNullException ("parameterType");
68 if (!CanConvert (parameterType))
69 throw new NotSupportedException (String.Format ("Conversion from the argument parameterType '{0}' is not supported", parameterType));
71 if (parameterType.IsEnum)
72 return Enum.Parse(parameterType, parameter, true);
74 switch (Type.GetTypeCode (parameterType)) {
79 return parameter != null ? Char.Parse (parameter) : default (char);
82 return parameter != null ? SByte.Parse (parameter, CultureInfo.InvariantCulture): default (sbyte);
84 return parameter != null ? Byte.Parse (parameter, CultureInfo.InvariantCulture): default (byte);
86 return parameter != null ? Int16.Parse (parameter, CultureInfo.InvariantCulture): default (short);
88 return parameter != null ? Int32.Parse (parameter, CultureInfo.InvariantCulture): default (int);
90 return parameter != null ? Int64.Parse (parameter, CultureInfo.InvariantCulture): default (long);
92 return parameter != null ? UInt16.Parse (parameter, CultureInfo.InvariantCulture): default (ushort);
94 return parameter != null ? UInt32.Parse (parameter, CultureInfo.InvariantCulture): default (uint);
96 return parameter != null ? UInt64.Parse (parameter, CultureInfo.InvariantCulture): default (ulong);
97 case TypeCode.DateTime:
98 return parameter != null ? DateTime.Parse (parameter, CultureInfo.InvariantCulture): default (DateTime);
99 case TypeCode.Boolean:
100 return parameter != null ? Boolean.Parse (parameter): default (bool);
101 case TypeCode.Single:
102 return parameter != null ? Single.Parse (parameter, CultureInfo.InvariantCulture): default (float);
103 case TypeCode.Double:
104 return parameter != null ? Double.Parse (parameter, CultureInfo.InvariantCulture): default (double);
105 case TypeCode.Decimal:
106 return parameter != null ? Decimal.Parse (parameter, CultureInfo.InvariantCulture): default (decimal);
107 case TypeCode.Object:
108 if (parameterType == typeof (TimeSpan))
109 return TimeSpan.Parse (parameter);
110 if (parameterType == typeof (DateTimeOffset))
111 return DateTimeOffset.Parse (parameter, CultureInfo.InvariantCulture);
112 if (parameterType == typeof (Guid))
113 return new Guid (parameter);
116 throw new NotSupportedException (String.Format ("Cannot convert parameter string '{0}' to parameter type '{1}'", parameter, parameterType));
119 public virtual string ConvertValueToString (object parameter, Type parameterType)
121 if (parameterType == null)
122 throw new ArgumentNullException ("parameterType");
123 if (parameterType.IsValueType && parameter == null)
124 throw new ArgumentNullException ("parameter");
126 if (parameter == null)
129 if (parameter.GetType () != parameterType)
130 throw new InvalidCastException (String.Format ("This QueryStringConverter does not support cast from {0} to {1}", parameter.GetType (), parameterType));
132 if (!CanConvert (parameterType))
133 throw new NotSupportedException (String.Format ("Conversion from the argument parameterType '{0}' is not supported", parameterType));
135 if (parameter is IFormattable)
136 ((IFormattable) parameter).ToString (null, CultureInfo.InvariantCulture);
137 return parameter.ToString ();