DbConstantExpression.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using Ant.Common;
  2. using Ant.Utility;
  3. using System;
  4. namespace Ant.DbExpressions
  5. {
  6. public class DbConstantExpression : DbExpression
  7. {
  8. object _value;
  9. Type _type;
  10. public static readonly DbConstantExpression Null = new DbConstantExpression(null);
  11. public static readonly DbConstantExpression StringEmpty = new DbConstantExpression(string.Empty);
  12. public static readonly DbConstantExpression One = new DbConstantExpression(1);
  13. public static readonly DbConstantExpression Zero = new DbConstantExpression(0);
  14. public static readonly DbConstantExpression True = new DbConstantExpression(true);
  15. public static readonly DbConstantExpression False = new DbConstantExpression(false);
  16. public DbConstantExpression(object value)
  17. : base(DbExpressionType.Constant)
  18. {
  19. this._value = value;
  20. if (value != null)
  21. this._type = value.GetType();
  22. else
  23. this._type = UtilConstants.TypeOfObject;
  24. }
  25. public DbConstantExpression(object value, Type type)
  26. : base(DbExpressionType.Constant)
  27. {
  28. AntUtils.CheckNull(type);
  29. if (value != null)
  30. {
  31. Type t = value.GetType();
  32. if (!type.IsAssignableFrom(t))
  33. throw new ArgumentException();
  34. }
  35. this._value = value;
  36. this._type = type;
  37. }
  38. public override Type Type { get { return this._type; } }
  39. public object Value { get { return this._value; } }
  40. public override T Accept<T>(DbExpressionVisitor<T> visitor)
  41. {
  42. return visitor.Visit(this);
  43. }
  44. }
  45. }