XPathMessageQueryCollection.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Dispatcher
  5. {
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ServiceModel.Channels;
  9. using System.Xml;
  10. public class XPathMessageQueryCollection : MessageQueryCollection
  11. {
  12. InverseQueryMatcher matcher;
  13. public XPathMessageQueryCollection()
  14. {
  15. this.matcher = new InverseQueryMatcher(false);
  16. }
  17. public override IEnumerable<KeyValuePair<MessageQuery, TResult>> Evaluate<TResult>(Message message)
  18. {
  19. if (message == null)
  20. {
  21. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("message");
  22. }
  23. if (typeof(TResult) == typeof(XPathResult) ||
  24. typeof(TResult) == typeof(string) ||
  25. typeof(TResult) == typeof(bool) ||
  26. typeof(TResult) == typeof(object))
  27. {
  28. return (IEnumerable<KeyValuePair<MessageQuery, TResult>>)(object)
  29. this.matcher.Evaluate<TResult>(message, false);
  30. }
  31. else
  32. {
  33. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("TResult",
  34. SR.GetString(SR.UnsupportedMessageQueryResultType, typeof(TResult)));
  35. }
  36. }
  37. public override IEnumerable<KeyValuePair<MessageQuery, TResult>> Evaluate<TResult>(MessageBuffer buffer)
  38. {
  39. if (buffer == null)
  40. {
  41. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("buffer");
  42. }
  43. if (typeof(TResult) == typeof(XPathResult) ||
  44. typeof(TResult) == typeof(string) ||
  45. typeof(TResult) == typeof(bool) ||
  46. typeof(TResult) == typeof(object))
  47. {
  48. return (IEnumerable<KeyValuePair<MessageQuery, TResult>>)(object)
  49. this.matcher.Evaluate<TResult>(buffer);
  50. }
  51. else
  52. {
  53. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("TResult",
  54. SR.GetString(SR.UnsupportedMessageQueryResultType, typeof(TResult)));
  55. }
  56. }
  57. protected override void InsertItem(int index, MessageQuery item)
  58. {
  59. if (item == null)
  60. {
  61. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("item");
  62. }
  63. if (!(item is XPathMessageQuery))
  64. {
  65. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("item");
  66. }
  67. base.InsertItem(index, item);
  68. XPathMessageQuery query = (XPathMessageQuery) item;
  69. this.matcher.Add(query.Expression, query.Namespaces, query, false);
  70. }
  71. protected override void RemoveItem(int index)
  72. {
  73. this.matcher.Remove((XPathMessageQuery) this[index]);
  74. base.RemoveItem(index);
  75. }
  76. protected override void SetItem(int index, MessageQuery item)
  77. {
  78. if (!(item is XPathMessageQuery))
  79. {
  80. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("item");
  81. }
  82. this.matcher.Remove((XPathMessageQuery) this[index]);
  83. XPathMessageQuery query = (XPathMessageQuery) item;
  84. base.SetItem(index, item);
  85. this.matcher.Add(query.Expression, query.Namespaces, query, false);
  86. }
  87. }
  88. }