MessageQueueConverter.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //------------------------------------------------------------------------------
  2. // <copyright file="MessageQueueConverter.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.Collections;
  9. using System.ComponentModel;
  10. using System.Globalization;
  11. namespace Experimental.System.Messaging.Design
  12. {
  13. /// <include file='doc\MessageQueueConverter.uex' path='docs/doc[@for="MessageQueueConverter"]/*' />
  14. /// <internalonly/>
  15. internal class MessageQueueConverter : TypeConverter
  16. {
  17. private static Hashtable componentsCreated = new Hashtable(StringComparer.InvariantCultureIgnoreCase);
  18. /// <include file='doc\MessageQueueConverter.uex' path='docs/doc[@for="MessageQueueConverter.CanConvertFrom"]/*' />
  19. /// <internalonly/>
  20. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  21. {
  22. if (sourceType == typeof(string))
  23. {
  24. return true;
  25. }
  26. return base.CanConvertFrom(context, sourceType);
  27. }
  28. internal static void AddToCache(MessageQueue queue)
  29. {
  30. componentsCreated[queue.Path] = queue;
  31. }
  32. /// <include file='doc\MessageQueueConverter.uex' path='docs/doc[@for="MessageQueueConverter.ConvertFrom"]/*' />
  33. /// <internalonly/>
  34. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  35. {
  36. if (value != null && value is string)
  37. {
  38. string text = ((string)value).Trim();
  39. if (text == String.Empty)
  40. return null;
  41. if (text.CompareTo(Res.GetString(Res.NotSet)) != 0)
  42. {
  43. MessageQueue newQueue = GetFromCache(text);
  44. if (newQueue != null)
  45. return newQueue;
  46. else
  47. {
  48. newQueue = new MessageQueue(text);
  49. AddToCache(newQueue);
  50. if (context != null)
  51. context.Container.Add(newQueue);
  52. return newQueue;
  53. }
  54. }
  55. }
  56. return null;
  57. }
  58. /// <include file='doc\MessageQueueConverter.uex' path='docs/doc[@for="MessageQueueConverter.ConvertTo"]/*' />
  59. /// <internalonly/>
  60. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  61. {
  62. if (destinationType != null && destinationType == typeof(string))
  63. {
  64. if (value != null)
  65. return ((MessageQueue)value).Path;
  66. else
  67. return Res.GetString(Res.NotSet);
  68. }
  69. return base.ConvertTo(context, culture, value, destinationType);
  70. }
  71. internal static MessageQueue GetFromCache(string path)
  72. {
  73. if (componentsCreated.ContainsKey(path))
  74. {
  75. MessageQueue existingComponent = (MessageQueue)componentsCreated[path];
  76. if (existingComponent.Site == null)
  77. componentsCreated.Remove(path);
  78. else
  79. {
  80. if (existingComponent.Path == path)
  81. return existingComponent;
  82. else
  83. componentsCreated.Remove(path);
  84. }
  85. }
  86. return null;
  87. }
  88. }
  89. }