Line data Source code
1 : #region Copyright
2 : // // -----------------------------------------------------------------------
3 : // // <copyright company="Chinchilla Software Limited">
4 : // // Copyright Chinchilla Software Limited. All rights reserved.
5 : // // </copyright>
6 : // // -----------------------------------------------------------------------
7 : #endregion
8 :
9 : using System;
10 : using System.ComponentModel;
11 : using System.Globalization;
12 :
13 : namespace Cqrs.WebApi.Formatters.FormMultipart.Infrastructure.TypeConverters
14 : {
15 : /// <summary>
16 : /// Provides a unified way of converting <see cref="string"/> values to other types with support for a textual "undefined" value referring to null.
17 : /// </summary>
18 : public class FromStringConverterAdapter
19 1 : {
20 : private readonly Type _type;
21 :
22 : private readonly TypeConverter _typeConverter;
23 :
24 : /// <summary>
25 : /// Instantiates a new instance of <see cref="FromStringConverterAdapter"/>.
26 : /// </summary>
27 1 : public FromStringConverterAdapter(Type type, TypeConverter typeConverter)
28 : {
29 : if(type == null)
30 : throw new ArgumentNullException("type");
31 : if (typeConverter == null)
32 : throw new ArgumentNullException("typeConverter");
33 :
34 : _type = type;
35 : _typeConverter = typeConverter;
36 : }
37 :
38 : /// <summary>
39 : /// Converts the given text to an object, using the specified context and culture information.
40 : /// </summary>
41 : /// <param name="culture">A <see cref="CultureInfo"/> that specifies the culture to which to convert.</param>
42 : /// <param name="value">The <see cref="string"/> to convert.</param>
43 : /// <returns>An <see cref="object"/> that represents the converted <paramref name="value"/>.</returns>
44 1 : public object ConvertFromString(string value, CultureInfo culture)
45 : {
46 : var isUndefinedNullable = Nullable.GetUnderlyingType(_type) != null && value == "undefined";
47 : if (isUndefinedNullable)
48 : return null;
49 :
50 : return _typeConverter.ConvertFromString(null, culture, value);
51 : }
52 : }
53 : }
|