| | | 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.ComponentModel; |
| | | 6 | | using System.ComponentModel.Design.Serialization; |
| | | 7 | | using System.Text; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.Configuration |
| | | 10 | | { |
| | | 11 | | internal class EncodingConverter : TypeConverter |
| | | 12 | | { |
| | | 13 | | public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) |
| | | 14 | | { |
| | 0 | 15 | | if (typeof(string) == sourceType) |
| | | 16 | | { |
| | 0 | 17 | | return true; |
| | | 18 | | } |
| | 0 | 19 | | return base.CanConvertFrom(context, sourceType); |
| | | 20 | | } |
| | | 21 | | |
| | | 22 | | public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) |
| | | 23 | | { |
| | 0 | 24 | | if (typeof(InstanceDescriptor) == destinationType) |
| | | 25 | | { |
| | 0 | 26 | | return true; |
| | | 27 | | } |
| | 0 | 28 | | return base.CanConvertTo(context, destinationType); |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, obj |
| | | 32 | | { |
| | 10 | 33 | | if (value is string) |
| | | 34 | | { |
| | 10 | 35 | | string encoding = (string)value; |
| | | 36 | | |
| | | 37 | | Encoding retval; |
| | 10 | 38 | | if (String.Compare(encoding, "utf-8", StringComparison.OrdinalIgnoreCase) == 0) |
| | | 39 | | { |
| | | 40 | | // special case for utf-8 to match with what we do in the default text encoding |
| | 7 | 41 | | retval = Encoding.UTF8; |
| | | 42 | | } |
| | | 43 | | else |
| | | 44 | | { |
| | 3 | 45 | | retval = Encoding.GetEncoding(encoding); |
| | | 46 | | } |
| | 10 | 47 | | if (retval == null) |
| | | 48 | | { |
| | 0 | 49 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(value), SR.ConfigInvalidEncoding |
| | | 50 | | } |
| | 10 | 51 | | return retval; |
| | | 52 | | } |
| | 0 | 53 | | return base.ConvertFrom(context, culture, value); |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, objec |
| | | 57 | | { |
| | 0 | 58 | | if (typeof(string) == destinationType && value is Encoding) |
| | | 59 | | { |
| | 0 | 60 | | Encoding encoding = (Encoding)value; |
| | 0 | 61 | | return encoding.HeaderName; |
| | | 62 | | } |
| | 0 | 63 | | return base.ConvertTo(context, culture, value, destinationType); |
| | | 64 | | } |
| | | 65 | | } |
| | | 66 | | } |