ReliableMessagingVersionConverter.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Configuration
  5. {
  6. //using System;
  7. using System.ComponentModel;
  8. using System.ComponentModel.Design.Serialization;
  9. using System.Globalization;
  10. class ReliableMessagingVersionConverter : TypeConverter
  11. {
  12. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  13. {
  14. if (typeof(string) == sourceType)
  15. {
  16. return true;
  17. }
  18. return base.CanConvertFrom(context, sourceType);
  19. }
  20. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  21. {
  22. if (typeof(InstanceDescriptor) == destinationType)
  23. {
  24. return true;
  25. }
  26. return base.CanConvertTo(context, destinationType);
  27. }
  28. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  29. {
  30. string version = value as string;
  31. #pragma warning suppress 56507 // [....], Really checking for null (meaning value was not a string) versus String.Empty
  32. if (version != null)
  33. {
  34. switch (version)
  35. {
  36. case ConfigurationStrings.Default:
  37. return ReliableMessagingVersion.Default;
  38. case ConfigurationStrings.WSReliableMessaging11:
  39. return ReliableMessagingVersion.WSReliableMessaging11;
  40. case ConfigurationStrings.WSReliableMessagingFebruary2005:
  41. return ReliableMessagingVersion.WSReliableMessagingFebruary2005;
  42. default:
  43. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.ConfigInvalidReliableMessagingVersionValue, version));
  44. }
  45. }
  46. return base.ConvertFrom(context, culture, value);
  47. }
  48. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  49. {
  50. if (typeof(string) == destinationType && value is ReliableMessagingVersion)
  51. {
  52. ReliableMessagingVersion version = (ReliableMessagingVersion)value;
  53. if (version == ReliableMessagingVersion.Default)
  54. {
  55. return ConfigurationStrings.Default;
  56. }
  57. else if (version == ReliableMessagingVersion.WSReliableMessaging11)
  58. {
  59. return ConfigurationStrings.WSReliableMessaging11;
  60. }
  61. else if (version == ReliableMessagingVersion.WSReliableMessagingFebruary2005)
  62. {
  63. return ConfigurationStrings.WSReliableMessagingFebruary2005;
  64. }
  65. else
  66. {
  67. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value",
  68. SR.GetString(SR.ConfigInvalidClassInstanceValue, typeof(ReliableMessagingVersion).FullName)));
  69. }
  70. }
  71. return base.ConvertTo(context, culture, value, destinationType);
  72. }
  73. }
  74. }