DiscoveryVersionConverter.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // Author: Atsushi Enomoto <[email protected]>
  3. //
  4. // Copyright (C) 2010 Novell, Inc (http://www.novell.com)
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining
  7. // a copy of this software and associated documentation files (the
  8. // "Software"), to deal in the Software without restriction, including
  9. // without limitation the rights to use, copy, modify, merge, publish,
  10. // distribute, sublicense, and/or sell copies of the Software, and to
  11. // permit persons to whom the Software is furnished to do so, subject to
  12. // the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be
  15. // included in all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. //
  25. #if NET_4_0
  26. using System;
  27. using System.ComponentModel;
  28. using System.Configuration;
  29. using System.Globalization;
  30. namespace System.ServiceModel.Discovery.Configuration
  31. {
  32. public class DiscoveryVersionConverter : TypeConverter
  33. {
  34. public DiscoveryVersionConverter ()
  35. {
  36. }
  37. private bool CanConvert (Type type)
  38. {
  39. if (type == typeof (string))
  40. return true;
  41. if (type == typeof (DiscoveryVersion))
  42. return true;
  43. return false;
  44. }
  45. public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
  46. {
  47. if (sourceType == null)
  48. throw new ArgumentNullException ("sourceType");
  49. return CanConvert (sourceType);
  50. }
  51. public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
  52. {
  53. if (destinationType == null)
  54. return false;
  55. return CanConvert (destinationType);
  56. }
  57. public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
  58. {
  59. if (value == null)
  60. throw new ArgumentNullException ("value");
  61. if (!CanConvertFrom (context, value.GetType ()))
  62. throw new NotSupportedException ("Cannot convert from value.");
  63. if (value is DiscoveryVersion)
  64. return value;
  65. string s = (value as string);
  66. if (s != null) {
  67. switch (s) {
  68. case "WSDiscovery11":
  69. return DiscoveryVersion.WSDiscovery11;
  70. case "WSDiscoveryApril2005":
  71. return DiscoveryVersion.WSDiscoveryApril2005;
  72. case "WSDiscoveryCD1":
  73. return DiscoveryVersion.WSDiscoveryCD1;
  74. }
  75. throw new NotSupportedException ("Cannot convert from value.");
  76. }
  77. return base.ConvertFrom (context, culture, value);
  78. }
  79. public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  80. {
  81. if (!CanConvertTo (context, destinationType))
  82. throw new NotSupportedException (Locale.GetText ("Cannot convert to destination type."));
  83. var ver = (value as DiscoveryVersion);
  84. if (ver != null) {
  85. if (destinationType == typeof (DiscoveryVersion))
  86. return ver;
  87. if (destinationType == typeof (string)) {
  88. if (ver.Equals (DiscoveryVersion.WSDiscovery11))
  89. return "WSDiscovery11";
  90. if (ver.Equals (DiscoveryVersion.WSDiscoveryApril2005))
  91. return "WSDiscoveryApril2005";
  92. if (ver.Equals (DiscoveryVersion.WSDiscoveryCD1))
  93. return "WSDiscoveryCD1";
  94. }
  95. throw new NotSupportedException ("Cannot convert to destination type.");
  96. }
  97. return base.ConvertTo (context, culture, value, destinationType);
  98. }
  99. }
  100. }
  101. #endif