UriTemplateVariableQueryValue.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. //----------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //----------------------------------------------------------------
  4. namespace System
  5. {
  6. using System.Collections.Specialized;
  7. using System.Runtime;
  8. using System.ServiceModel.Channels;
  9. using System.Text;
  10. class UriTemplateVariableQueryValue : UriTemplateQueryValue
  11. {
  12. readonly string varName;
  13. public UriTemplateVariableQueryValue(string varName)
  14. : base(UriTemplatePartType.Variable)
  15. {
  16. Fx.Assert(!string.IsNullOrEmpty(varName), "bad variable segment");
  17. this.varName = varName;
  18. }
  19. public override void Bind(string keyName, string[] values, ref int valueIndex, StringBuilder query)
  20. {
  21. Fx.Assert(valueIndex < values.Length, "Not enough values to bind");
  22. if (values[valueIndex] == null)
  23. {
  24. valueIndex++;
  25. }
  26. else
  27. {
  28. query.AppendFormat("&{0}={1}", UrlUtility.UrlEncode(keyName, Encoding.UTF8), UrlUtility.UrlEncode(values[valueIndex++], Encoding.UTF8));
  29. }
  30. }
  31. public override bool IsEquivalentTo(UriTemplateQueryValue other)
  32. {
  33. if (other == null)
  34. {
  35. Fx.Assert("why would we ever call this?");
  36. return false;
  37. }
  38. return (other.Nature == UriTemplatePartType.Variable);
  39. }
  40. public override void Lookup(string value, NameValueCollection boundParameters)
  41. {
  42. boundParameters.Add(this.varName, value);
  43. }
  44. }
  45. }