ArrayIndexExpression.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* Copyright 2015-present MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Linq.Expressions;
  17. using MongoDB.Bson.Serialization;
  18. using MongoDB.Driver.Core.Misc;
  19. namespace MongoDB.Driver.Linq.Expressions
  20. {
  21. internal sealed class ArrayIndexExpression : SerializationExpression
  22. {
  23. private readonly Expression _array;
  24. private readonly Expression _index;
  25. private readonly Expression _original;
  26. private readonly IBsonSerializer _serializer;
  27. public ArrayIndexExpression(Expression array, Expression index, IBsonSerializer serializer)
  28. : this(array, index, serializer, null)
  29. {
  30. }
  31. public ArrayIndexExpression(Expression array, Expression index, IBsonSerializer serializer, Expression original)
  32. {
  33. _array = Ensure.IsNotNull(array, nameof(array));
  34. _index = Ensure.IsNotNull(index, nameof(index));
  35. _serializer = Ensure.IsNotNull(serializer, nameof(serializer));
  36. _original = original;
  37. }
  38. public Expression Array
  39. {
  40. get { return _array; }
  41. }
  42. public Expression Index
  43. {
  44. get { return _index; }
  45. }
  46. public override ExtensionExpressionType ExtensionType
  47. {
  48. get { return ExtensionExpressionType.ArrayIndex; }
  49. }
  50. public Expression Original
  51. {
  52. get { return _original; }
  53. }
  54. public override IBsonSerializer Serializer
  55. {
  56. get { return _serializer; }
  57. }
  58. public override Type Type
  59. {
  60. get { return _serializer.ValueType; }
  61. }
  62. public override string ToString()
  63. {
  64. return _array.ToString() + "[" + _index.ToString() + "]";
  65. }
  66. public ArrayIndexExpression Update(Expression array, Expression index, Expression original)
  67. {
  68. if (array != _array || index != _index || original != _original)
  69. {
  70. return new ArrayIndexExpression(array, index, _serializer, original);
  71. }
  72. return this;
  73. }
  74. protected internal override Expression Accept(ExtensionExpressionVisitor visitor)
  75. {
  76. return visitor.VisitArrayIndex(this);
  77. }
  78. }
  79. }