MessageFormatterConverter.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //------------------------------------------------------------------------------
  2. // <copyright file="MessageFormatterConverter.cs" company="Microsoft">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. //------------------------------------------------------------------------------
  6. using Experimental.System.Messaging;
  7. using System;
  8. using System.ComponentModel;
  9. using System.ComponentModel.Design.Serialization;
  10. using System.Globalization;
  11. using System.Reflection;
  12. using System.Runtime.Serialization.Formatters;
  13. namespace Experimental.System.Messaging.Design
  14. {
  15. /// <include file='doc\MessageFormatterConverter.uex' path='docs/doc[@for="MessageFormatterConverter"]/*' />
  16. /// <internalonly/>
  17. internal class MessageFormatterConverter : ExpandableObjectConverter
  18. {
  19. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  20. {
  21. return (sourceType == typeof(string));
  22. }
  23. /// <include file='doc\MessageFormatterConverter.uex' path='docs/doc[@for="MessageFormatterConverter.CanConvertTo"]/*' />
  24. /// <devdoc>
  25. /// <para>Gets a value indicating whether this converter can
  26. /// convert an object to the given destination type using the context.</para>
  27. /// </devdoc>
  28. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  29. {
  30. if (destinationType == typeof(InstanceDescriptor))
  31. {
  32. return true;
  33. }
  34. return base.CanConvertTo(context, destinationType);
  35. }
  36. /// <include file='doc\MessageFormatterConverter.uex' path='docs/doc[@for="MessageFormatterConverter.ConvertFrom"]/*' />
  37. /// <internalonly/>
  38. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  39. {
  40. if (value != null && value is string)
  41. {
  42. if (((string)value) == typeof(ActiveXMessageFormatter).Name)
  43. return new ActiveXMessageFormatter();
  44. if (((string)value) == typeof(BinaryMessageFormatter).Name)
  45. return new BinaryMessageFormatter();
  46. if (((string)value) == typeof(XmlMessageFormatter).Name)
  47. return new XmlMessageFormatter();
  48. }
  49. return null;
  50. }
  51. /// <include file='doc\MessageFormatterConverter.uex' path='docs/doc[@for="MessageFormatterConverter.ConvertTo"]/*' />
  52. /// <internalonly/>
  53. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  54. {
  55. if (destinationType != null && destinationType == typeof(string))
  56. {
  57. if (value == null)
  58. return Res.GetString(Res.toStringNone);
  59. return value.GetType().Name;
  60. }
  61. if (destinationType == typeof(InstanceDescriptor))
  62. {
  63. if (value is XmlMessageFormatter)
  64. {
  65. XmlMessageFormatter f = (XmlMessageFormatter)value;
  66. ConstructorInfo ctor = typeof(XmlMessageFormatter).GetConstructor(new Type[] { typeof(string[]) });
  67. if (ctor != null)
  68. {
  69. return new InstanceDescriptor(ctor, new object[] { f.TargetTypeNames });
  70. }
  71. }
  72. else if (value is ActiveXMessageFormatter)
  73. {
  74. ConstructorInfo ctor = typeof(ActiveXMessageFormatter).GetConstructor(new Type[0]);
  75. if (ctor != null)
  76. {
  77. return new InstanceDescriptor(ctor, new object[0]);
  78. }
  79. }
  80. else if (value is BinaryMessageFormatter)
  81. {
  82. BinaryMessageFormatter f = (BinaryMessageFormatter)value;
  83. ConstructorInfo ctor = typeof(BinaryMessageFormatter).GetConstructor(new Type[] {
  84. typeof(FormatterAssemblyStyle), typeof(FormatterTypeStyle) });
  85. if (ctor != null)
  86. {
  87. return new InstanceDescriptor(ctor, new object[] { f.TopObjectFormat, f.TypeFormat });
  88. }
  89. }
  90. }
  91. return base.ConvertTo(context, culture, value, destinationType);
  92. }
  93. /// <include file='doc\MessageFormatterConverter.uex' path='docs/doc[@for="MessageFormatterConverter.GetStandardValues"]/*' />
  94. /// <internalonly/>
  95. public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
  96. {
  97. StandardValuesCollection values = new StandardValuesCollection(new object[] { new ActiveXMessageFormatter(),
  98. new BinaryMessageFormatter(),
  99. new XmlMessageFormatter(),
  100. null });
  101. return values;
  102. }
  103. public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
  104. {
  105. return true;
  106. }
  107. /// <include file='doc\MessageFormatterConverter.uex' path='docs/doc[@for="MessageFormatterConverter.GetStandardValuesSupported"]/*' />
  108. /// <internalonly/>
  109. public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
  110. {
  111. return true;
  112. }
  113. }
  114. }