| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections; |
| | | 6 | | using System.ComponentModel; |
| | | 7 | | using System.Diagnostics; |
| | | 8 | | using System.Globalization; |
| | | 9 | | using System.Reflection; |
| | | 10 | | using System.Runtime.InteropServices; |
| | | 11 | | using System.Xml; |
| | | 12 | | using CoreWCF.Runtime; |
| | | 13 | | |
| | | 14 | | namespace CoreWCF.Dispatcher |
| | | 15 | | { |
| | | 16 | | public class QueryStringConverter |
| | | 17 | | { |
| | | 18 | | private readonly Hashtable _defaultSupportedQueryStringTypes; |
| | | 19 | | |
| | | 20 | | // the cache does not have a quota since it is per endpoint and is |
| | | 21 | | // bounded by the number of types in the contract at the endpoint |
| | | 22 | | private readonly Hashtable _typeConverterCache; |
| | | 23 | | |
| | 326 | 24 | | public QueryStringConverter() |
| | | 25 | | { |
| | 326 | 26 | | _defaultSupportedQueryStringTypes = new Hashtable(); |
| | 326 | 27 | | _defaultSupportedQueryStringTypes.Add(typeof(byte), null); |
| | 326 | 28 | | _defaultSupportedQueryStringTypes.Add(typeof(sbyte), null); |
| | 326 | 29 | | _defaultSupportedQueryStringTypes.Add(typeof(short), null); |
| | 326 | 30 | | _defaultSupportedQueryStringTypes.Add(typeof(int), null); |
| | 326 | 31 | | _defaultSupportedQueryStringTypes.Add(typeof(long), null); |
| | 326 | 32 | | _defaultSupportedQueryStringTypes.Add(typeof(ushort), null); |
| | 326 | 33 | | _defaultSupportedQueryStringTypes.Add(typeof(uint), null); |
| | 326 | 34 | | _defaultSupportedQueryStringTypes.Add(typeof(ulong), null); |
| | 326 | 35 | | _defaultSupportedQueryStringTypes.Add(typeof(float), null); |
| | 326 | 36 | | _defaultSupportedQueryStringTypes.Add(typeof(double), null); |
| | 326 | 37 | | _defaultSupportedQueryStringTypes.Add(typeof(bool), null); |
| | 326 | 38 | | _defaultSupportedQueryStringTypes.Add(typeof(char), null); |
| | 326 | 39 | | _defaultSupportedQueryStringTypes.Add(typeof(decimal), null); |
| | 326 | 40 | | _defaultSupportedQueryStringTypes.Add(typeof(string), null); |
| | 326 | 41 | | _defaultSupportedQueryStringTypes.Add(typeof(object), null); |
| | 326 | 42 | | _defaultSupportedQueryStringTypes.Add(typeof(DateTime), null); |
| | 326 | 43 | | _defaultSupportedQueryStringTypes.Add(typeof(TimeSpan), null); |
| | 326 | 44 | | _defaultSupportedQueryStringTypes.Add(typeof(byte[]), null); |
| | 326 | 45 | | _defaultSupportedQueryStringTypes.Add(typeof(Guid), null); |
| | 326 | 46 | | _defaultSupportedQueryStringTypes.Add(typeof(Uri), null); |
| | 326 | 47 | | _defaultSupportedQueryStringTypes.Add(typeof(DateTimeOffset), null); |
| | 326 | 48 | | _typeConverterCache = new Hashtable(); |
| | 326 | 49 | | } |
| | | 50 | | |
| | | 51 | | public virtual bool CanConvert(Type type) |
| | | 52 | | { |
| | 16 | 53 | | if (_defaultSupportedQueryStringTypes.ContainsKey(type)) |
| | | 54 | | { |
| | 16 | 55 | | return true; |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | // otherwise check if its an enum |
| | 0 | 59 | | if (typeof(Enum).IsAssignableFrom(type)) |
| | | 60 | | { |
| | 0 | 61 | | return true; |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | // check if there's a typeconverter defined on the type |
| | 0 | 65 | | return (GetStringConverter(type) != null); |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | public virtual object ConvertStringToValue(string parameter, Type parameterType) |
| | | 69 | | { |
| | 1 | 70 | | if (parameterType == null) |
| | | 71 | | { |
| | 0 | 72 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(parameterType)); |
| | | 73 | | } |
| | | 74 | | |
| | 1 | 75 | | switch (Type.GetTypeCode(parameterType)) |
| | | 76 | | { |
| | | 77 | | case TypeCode.Byte: |
| | 0 | 78 | | return parameter == null ? default : XmlConvert.ToByte(parameter); |
| | | 79 | | case TypeCode.SByte: |
| | 0 | 80 | | return parameter == null ? default : XmlConvert.ToSByte(parameter); |
| | | 81 | | case TypeCode.Int16: |
| | 0 | 82 | | return parameter == null ? default : XmlConvert.ToInt16(parameter); |
| | | 83 | | case TypeCode.Int32: |
| | | 84 | | { |
| | 0 | 85 | | if (typeof(Enum).IsAssignableFrom(parameterType)) |
| | | 86 | | { |
| | 0 | 87 | | return Enum.Parse(parameterType, parameter, true); |
| | | 88 | | } |
| | | 89 | | else |
| | | 90 | | { |
| | 0 | 91 | | return parameter == null ? default : XmlConvert.ToInt32(parameter); |
| | | 92 | | } |
| | | 93 | | } |
| | | 94 | | case TypeCode.Int64: |
| | 0 | 95 | | return parameter == null ? default : XmlConvert.ToInt64(parameter); |
| | | 96 | | case TypeCode.UInt16: |
| | 0 | 97 | | return parameter == null ? default : XmlConvert.ToUInt16(parameter); |
| | | 98 | | case TypeCode.UInt32: |
| | 0 | 99 | | return parameter == null ? default : XmlConvert.ToUInt32(parameter); |
| | | 100 | | case TypeCode.UInt64: |
| | 0 | 101 | | return parameter == null ? default : XmlConvert.ToUInt64(parameter); |
| | | 102 | | case TypeCode.Single: |
| | 0 | 103 | | return parameter == null ? default : XmlConvert.ToSingle(parameter); |
| | | 104 | | case TypeCode.Double: |
| | 0 | 105 | | return parameter == null ? default : XmlConvert.ToDouble(parameter); |
| | | 106 | | case TypeCode.Char: |
| | 0 | 107 | | return parameter == null ? default : XmlConvert.ToChar(parameter); |
| | | 108 | | case TypeCode.Decimal: |
| | 0 | 109 | | return parameter == null ? default : XmlConvert.ToDecimal(parameter); |
| | | 110 | | case TypeCode.Boolean: |
| | 0 | 111 | | return parameter == null ? default : Convert.ToBoolean(parameter, CultureInfo.InvariantCulture); |
| | | 112 | | case TypeCode.String: |
| | 1 | 113 | | return parameter; |
| | | 114 | | case TypeCode.DateTime: |
| | 0 | 115 | | return parameter == null ? default : DateTime.Parse(parameter, CultureInfo.InvariantCulture, DateTim |
| | | 116 | | default: |
| | | 117 | | { |
| | 0 | 118 | | if (parameterType == typeof(TimeSpan)) |
| | | 119 | | { |
| | | 120 | | // support the XML as well as default way of representing timespans |
| | | 121 | | TimeSpan result; |
| | 0 | 122 | | if (!TimeSpan.TryParse(parameter, out result)) |
| | | 123 | | { |
| | 0 | 124 | | result = parameter == null ? default : XmlConvert.ToTimeSpan(parameter); |
| | | 125 | | } |
| | 0 | 126 | | return result; |
| | | 127 | | } |
| | 0 | 128 | | else if (parameterType == typeof(Guid)) |
| | | 129 | | { |
| | 0 | 130 | | return parameter == null ? default : XmlConvert.ToGuid(parameter); |
| | | 131 | | } |
| | 0 | 132 | | else if (parameterType == typeof(DateTimeOffset)) |
| | | 133 | | { |
| | 0 | 134 | | return (parameter == null) ? default : DateTimeOffset.Parse(parameter, CultureInfo.Invariant |
| | | 135 | | } |
| | 0 | 136 | | else if (parameterType == typeof(byte[])) |
| | | 137 | | { |
| | 0 | 138 | | return (!string.IsNullOrEmpty(parameter)) ? Convert.FromBase64String(parameter) : new byte[] |
| | | 139 | | } |
| | 0 | 140 | | else if (parameterType == typeof(Uri)) |
| | | 141 | | { |
| | 0 | 142 | | return (!string.IsNullOrEmpty(parameter)) ? new Uri(parameter, UriKind.RelativeOrAbsolute) : |
| | | 143 | | } |
| | 0 | 144 | | else if (parameterType == typeof(object)) |
| | | 145 | | { |
| | 0 | 146 | | return parameter; |
| | | 147 | | } |
| | | 148 | | else |
| | | 149 | | { |
| | 0 | 150 | | TypeConverter stringConverter = GetStringConverter(parameterType); |
| | 0 | 151 | | if (stringConverter == null) |
| | | 152 | | { |
| | 0 | 153 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException( |
| | 0 | 154 | | SR.Format( |
| | 0 | 155 | | SR.TypeNotSupportedByQueryStringConverter, |
| | 0 | 156 | | parameterType.ToString(), GetType().Name))); |
| | | 157 | | } |
| | | 158 | | |
| | 0 | 159 | | return stringConverter.ConvertFromInvariantString(parameter); |
| | | 160 | | } |
| | | 161 | | } |
| | | 162 | | } |
| | | 163 | | } |
| | | 164 | | |
| | | 165 | | public virtual string ConvertValueToString(object parameter, Type parameterType) |
| | | 166 | | { |
| | 0 | 167 | | if (parameterType == null) |
| | | 168 | | { |
| | 0 | 169 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(parameterType)); |
| | | 170 | | } |
| | | 171 | | |
| | 0 | 172 | | if (parameterType.IsValueType && parameter == null) |
| | | 173 | | { |
| | 0 | 174 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(parameter)); |
| | | 175 | | } |
| | | 176 | | |
| | 0 | 177 | | switch (Type.GetTypeCode(parameterType)) |
| | | 178 | | { |
| | | 179 | | case TypeCode.Byte: |
| | 0 | 180 | | return XmlConvert.ToString((byte)parameter); |
| | | 181 | | case TypeCode.SByte: |
| | 0 | 182 | | return XmlConvert.ToString((sbyte)parameter); |
| | | 183 | | case TypeCode.Int16: |
| | 0 | 184 | | return XmlConvert.ToString((short)parameter); |
| | | 185 | | case TypeCode.Int32: |
| | | 186 | | { |
| | 0 | 187 | | if (typeof(Enum).IsAssignableFrom(parameterType)) |
| | | 188 | | { |
| | 0 | 189 | | return Enum.Format(parameterType, parameter, "G"); |
| | | 190 | | } |
| | | 191 | | else |
| | | 192 | | { |
| | 0 | 193 | | return XmlConvert.ToString((int)parameter); |
| | | 194 | | } |
| | | 195 | | } |
| | | 196 | | case TypeCode.Int64: |
| | 0 | 197 | | return XmlConvert.ToString((long)parameter); |
| | | 198 | | case TypeCode.UInt16: |
| | 0 | 199 | | return XmlConvert.ToString((ushort)parameter); |
| | | 200 | | case TypeCode.UInt32: |
| | 0 | 201 | | return XmlConvert.ToString((uint)parameter); |
| | | 202 | | case TypeCode.UInt64: |
| | 0 | 203 | | return XmlConvert.ToString((ulong)parameter); |
| | | 204 | | case TypeCode.Single: |
| | 0 | 205 | | return XmlConvert.ToString((float)parameter); |
| | | 206 | | case TypeCode.Double: |
| | 0 | 207 | | return XmlConvert.ToString((double)parameter); |
| | | 208 | | case TypeCode.Char: |
| | 0 | 209 | | return XmlConvert.ToString((char)parameter); |
| | | 210 | | case TypeCode.Decimal: |
| | 0 | 211 | | return XmlConvert.ToString((decimal)parameter); |
| | | 212 | | case TypeCode.Boolean: |
| | 0 | 213 | | return XmlConvert.ToString((bool)parameter); |
| | | 214 | | case TypeCode.String: |
| | 0 | 215 | | return (string)parameter; |
| | | 216 | | case TypeCode.DateTime: |
| | 0 | 217 | | return XmlConvert.ToString((DateTime)parameter, XmlDateTimeSerializationMode.RoundtripKind); |
| | | 218 | | default: |
| | | 219 | | { |
| | 0 | 220 | | if (parameterType == typeof(TimeSpan)) |
| | | 221 | | { |
| | 0 | 222 | | return XmlConvert.ToString((TimeSpan)parameter); |
| | | 223 | | } |
| | 0 | 224 | | else if (parameterType == typeof(Guid)) |
| | | 225 | | { |
| | 0 | 226 | | return XmlConvert.ToString((Guid)parameter); |
| | | 227 | | } |
| | 0 | 228 | | else if (parameterType == typeof(DateTimeOffset)) |
| | | 229 | | { |
| | 0 | 230 | | return XmlConvert.ToString((DateTimeOffset)parameter); |
| | | 231 | | } |
| | 0 | 232 | | else if (parameterType == typeof(byte[])) |
| | | 233 | | { |
| | 0 | 234 | | return (parameter != null) ? Convert.ToBase64String((byte[])parameter, Base64FormattingOptio |
| | | 235 | | } |
| | 0 | 236 | | else if (parameterType == typeof(Uri) || parameterType == typeof(object)) |
| | | 237 | | { |
| | | 238 | | // URI or object |
| | 0 | 239 | | return (parameter != null) ? Convert.ToString(parameter, CultureInfo.InvariantCulture) : nul |
| | | 240 | | } |
| | | 241 | | else |
| | | 242 | | { |
| | 0 | 243 | | TypeConverter stringConverter = GetStringConverter(parameterType); |
| | 0 | 244 | | if (stringConverter == null) |
| | | 245 | | { |
| | 0 | 246 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException( |
| | 0 | 247 | | SR.Format( |
| | 0 | 248 | | SR.TypeNotSupportedByQueryStringConverter, |
| | 0 | 249 | | parameterType.ToString(), GetType().Name))); |
| | | 250 | | } |
| | | 251 | | else |
| | | 252 | | { |
| | 0 | 253 | | return stringConverter.ConvertToInvariantString(parameter); |
| | | 254 | | } |
| | | 255 | | } |
| | | 256 | | } |
| | | 257 | | } |
| | | 258 | | } |
| | | 259 | | |
| | | 260 | | // hash table is safe for multiple readers single writer |
| | | 261 | | private TypeConverter GetStringConverter(Type parameterType) |
| | | 262 | | { |
| | 0 | 263 | | if (_typeConverterCache.ContainsKey(parameterType)) |
| | | 264 | | { |
| | 0 | 265 | | return (TypeConverter)_typeConverterCache[parameterType]; |
| | | 266 | | } |
| | | 267 | | |
| | 0 | 268 | | if (parameterType.GetCustomAttributes(typeof(TypeConverterAttribute), true) is TypeConverterAttribute[] type |
| | | 269 | | { |
| | 0 | 270 | | foreach (TypeConverterAttribute converterAttr in typeConverterAttrs) |
| | | 271 | | { |
| | 0 | 272 | | Type converterType = Type.GetType(converterAttr.ConverterTypeName, false, true); |
| | 0 | 273 | | if (converterType != null) |
| | | 274 | | { |
| | 0 | 275 | | TypeConverter converter = null; |
| | 0 | 276 | | Exception handledException = null; |
| | | 277 | | try |
| | | 278 | | { |
| | 0 | 279 | | converter = (TypeConverter)Activator.CreateInstance(converterType); |
| | 0 | 280 | | } |
| | | 281 | | catch (TargetInvocationException e) |
| | | 282 | | { |
| | 0 | 283 | | handledException = e; |
| | 0 | 284 | | } |
| | | 285 | | catch (MemberAccessException e) |
| | | 286 | | { |
| | 0 | 287 | | handledException = e; |
| | 0 | 288 | | } |
| | | 289 | | catch (TypeLoadException e) |
| | | 290 | | { |
| | 0 | 291 | | handledException = e; |
| | 0 | 292 | | } |
| | | 293 | | catch (COMException e) |
| | | 294 | | { |
| | 0 | 295 | | handledException = e; |
| | 0 | 296 | | } |
| | | 297 | | catch (InvalidComObjectException e) |
| | | 298 | | { |
| | 0 | 299 | | handledException = e; |
| | 0 | 300 | | } |
| | | 301 | | finally |
| | | 302 | | { |
| | 0 | 303 | | if (handledException != null) |
| | | 304 | | { |
| | 0 | 305 | | if (Fx.IsFatal(handledException)) |
| | | 306 | | { |
| | 0 | 307 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(handledException); |
| | | 308 | | } |
| | 0 | 309 | | DiagnosticUtility.TraceHandledException(handledException, TraceEventType.Warning); |
| | | 310 | | } |
| | 0 | 311 | | } |
| | | 312 | | |
| | 0 | 313 | | if (converter == null) |
| | | 314 | | { |
| | | 315 | | continue; |
| | | 316 | | } |
| | | 317 | | |
| | 0 | 318 | | if (converter.CanConvertTo(typeof(string)) && converter.CanConvertFrom(typeof(string))) |
| | | 319 | | { |
| | 0 | 320 | | _typeConverterCache.Add(parameterType, converter); |
| | 0 | 321 | | return converter; |
| | | 322 | | } |
| | | 323 | | } |
| | | 324 | | } |
| | | 325 | | } |
| | | 326 | | |
| | 0 | 327 | | return null; |
| | | 328 | | } |
| | | 329 | | } |
| | | 330 | | } |