| | | 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 |
| | | 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 | | } |
| | | 19 | | |
| | 0 | 20 | | return base.CanConvertFrom(context, sourceType); |
| | | 21 | | } |
| | | 22 | | |
| | | 23 | | public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) |
| | | 24 | | { |
| | 0 | 25 | | if (typeof(InstanceDescriptor) == destinationType) |
| | | 26 | | { |
| | 0 | 27 | | return true; |
| | | 28 | | } |
| | | 29 | | |
| | 0 | 30 | | return base.CanConvertTo(context, destinationType); |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, obj |
| | | 34 | | { |
| | 0 | 35 | | if (value is string encoding) |
| | | 36 | | { |
| | | 37 | | Encoding retval; |
| | 0 | 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 |
| | 0 | 41 | | retval = Encoding.UTF8; |
| | | 42 | | } |
| | | 43 | | else |
| | | 44 | | { |
| | 0 | 45 | | retval = Encoding.GetEncoding(encoding); |
| | | 46 | | } |
| | 0 | 47 | | if (retval == null) |
| | | 48 | | { |
| | 0 | 49 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(value), SR.ConfigInvalidEncoding |
| | | 50 | | } |
| | 0 | 51 | | return retval; |
| | | 52 | | } |
| | | 53 | | |
| | 0 | 54 | | return base.ConvertFrom(context, culture, value); |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, objec |
| | | 58 | | { |
| | 0 | 59 | | if (typeof(string) == destinationType && value is Encoding) |
| | | 60 | | { |
| | 0 | 61 | | Encoding encoding = (Encoding)value; |
| | 0 | 62 | | return encoding.HeaderName; |
| | | 63 | | } |
| | | 64 | | |
| | 0 | 65 | | return base.ConvertTo(context, culture, value, destinationType); |
| | | 66 | | } |
| | | 67 | | } |
| | | 68 | | } |