PageRenderTime 14ms CodeModel.GetById 1ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/ExpressInteropBinding/Microsoft.ServiceModel.Interop/Configuration/EncodingConverter.cs

#
C# | 100 lines | 51 code | 12 blank | 37 comment | 14 complexity | 371b336ad43b252712230f71faa09eca MD5 | raw file
  1// <copyright file="EncodingConverter.cs" company="Microsoft Corporation">
  2// Copyright (c) Microsoft Corporation.  All rights reserved.
  3// </copyright>
  4
  5namespace Microsoft.ServiceModel.Interop.Configuration
  6{
  7    using System;
  8    using System.ComponentModel;
  9    using System.ComponentModel.Design.Serialization;
 10    using System.Globalization;
 11    using System.Text;
 12    using Microsoft.ServiceModel.Interop.Properties;
 13
 14    /// <summary>
 15    /// Type converter implementation for text encodings
 16    /// </summary>
 17    internal class EncodingConverter : TypeConverter
 18    {
 19        /// <summary>
 20        /// Returns whether this converter can convert an object of the given type to
 21        //  the type of this converter.
 22        /// </summary>
 23        /// <param name="context">An System.ComponentModel.ITypeDescriptorContext that provides a format context.</param>
 24        /// <param name="sourceType">A System.Type that represents the type you want to convert from.</param>
 25        /// <returns>true if this converter can perform the conversion; otherwise, false.</returns>
 26        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
 27        {
 28            return (typeof(string) == sourceType) || base.CanConvertFrom(context, sourceType);
 29        }
 30
 31        /// <summary>
 32        ///  Returns whether this converter can convert the object to the specified type,
 33        //   using the specified context.
 34        /// </summary>
 35        /// <param name="context"> An System.ComponentModel.ITypeDescriptorContext that provides a format context.</param>
 36        /// <param name="destinationType">A System.Type that represents the type you want to convert to.</param>
 37        /// <returns>true if this converter can perform the conversion; otherwise, false.</returns>
 38        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
 39        {
 40            return (typeof(InstanceDescriptor) == destinationType) || base.CanConvertTo(context, destinationType);
 41        }
 42
 43        /// <summary>
 44        /// Converts the given object to the type of this converter, using the specified
 45        //  context and culture information.
 46        /// </summary>
 47        /// <param name="context">An System.ComponentModel.ITypeDescriptorContext that provides a format context.</param>
 48        /// <param name="culture">The System.Globalization.CultureInfo to use as the current culture.</param>
 49        /// <param name="value">The System.Object to convert.</param>
 50        /// <returns>An System.Object that represents the converted value.</returns>
 51        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
 52        {
 53            string stringValue = value as string;
 54
 55            Encoding encoding;
 56            if (stringValue == null)
 57            {
 58                return base.ConvertFrom(context, culture, value);
 59            }
 60
 61            if (string.Compare(stringValue, "utf-8", StringComparison.OrdinalIgnoreCase) == 0)
 62            {
 63                encoding = Encoding.GetEncoding("utf-8", new EncoderExceptionFallback(), new DecoderExceptionFallback());
 64            }
 65            else
 66            {
 67                encoding = Encoding.GetEncoding(stringValue);
 68            }
 69
 70            if (encoding == null)
 71            {
 72                throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, Strings.Invalid_Encoding, stringValue));
 73            }
 74
 75            return encoding;
 76        }
 77
 78        /// <summary>
 79        /// Converts the given value object to the specified type, using the specified
 80        //  context and culture information.
 81        /// </summary>
 82        /// <param name="context">An System.ComponentModel.ITypeDescriptorContext that provides a format context.</param>
 83        /// <param name="culture">A System.Globalization.CultureInfo. If null is passed, the current culture is assumed.</param>
 84        /// <param name="value">The System.Object to convert.</param>
 85        /// <param name="destinationType">The System.Type to convert the value parameter to.</param>
 86        /// <returns>An System.Object that represents the converted value.</returns>
 87        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
 88        {
 89            Encoding encodingValue = value as Encoding;
 90
 91            if ((typeof(string) == destinationType) && (encodingValue != null))
 92            {
 93                return encodingValue.HeaderName;
 94            }
 95
 96            return base.ConvertTo(context, culture, value, destinationType);
 97        }
 98    }
 99}
100