NilBehavior.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. namespace ClaySharp.Behaviors {
  6. public class NilBehavior : ClayBehavior {
  7. public override object GetMember(Func<object> proceed, object self, string name) {
  8. return Nil.Instance;
  9. }
  10. public override object GetIndex(Func<object> proceed, object self, IEnumerable<object> keys) {
  11. return Nil.Instance;
  12. }
  13. public override object InvokeMember(Func<object> proceed, object self, string name, INamedEnumerable<object> args) {
  14. if (args.Any())
  15. return proceed();
  16. if (name == "ToString") {
  17. return string.Empty;
  18. }
  19. return Nil.Instance;
  20. }
  21. public override object Convert(Func<object> proceed, object self, Type type, bool isExplicit) {
  22. if (type.IsInterface)
  23. return proceed();
  24. return null;
  25. }
  26. public override object BinaryOperation(Func<object> proceed, object self, ExpressionType operation, object value) {
  27. switch (operation) {
  28. case ExpressionType.Equal:
  29. return ReferenceEquals(value, Nil.Instance) || value == null;
  30. case ExpressionType.NotEqual:
  31. return !ReferenceEquals(value, Nil.Instance) && value != null;
  32. }
  33. return proceed();
  34. }
  35. }
  36. }