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