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 <see cref="TypeConverter"/> to convert <see cref="bool"/> objects to and from various other representations
17 : /// with support for HTTP specification specific values such as ON or OFF.
18 : /// </summary>
19 : public class BooleanConverterEx : BooleanConverter
20 1 : {
21 : /// <summary>
22 : /// Converts the given <paramref name="value"/> object to a <see cref="bool"/> object.
23 : /// </summary>
24 : /// <param name="context">An <see cref="ITypeDescriptorContext"/> that provides a format context.</param>
25 : /// <param name="culture">A <see cref="CultureInfo"/> that specifies the culture to which to convert.</param>
26 : /// <param name="value">The <see cref="object"/> to convert.</param>
27 : /// <returns>An <see cref="object"/> that represents the converted <paramref name="value"/>.</returns>
28 1 : public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
29 : {
30 : if (value != null)
31 : {
32 : var str = value.ToString();
33 :
34 : if (String.Compare(str, "on", culture, CompareOptions.IgnoreCase) == 0)
35 : return true;
36 :
37 : if (String.Compare(str, "off", culture, CompareOptions.IgnoreCase) == 0)
38 : return false;
39 : }
40 :
41 : return base.ConvertFrom(context, culture, value);
42 : }
43 : }
44 : }
|