using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; namespace ClaySharp.Behaviors { public class NilBehavior : ClayBehavior { public override object GetMember(Func proceed, object self, string name) { return Nil.Instance; } public override object GetIndex(Func proceed, object self, IEnumerable keys) { return Nil.Instance; } public override object InvokeMember(Func proceed, object self, string name, INamedEnumerable args) { if (args.Any()) return proceed(); if (name == "ToString") { return string.Empty; } return Nil.Instance; } public override object Convert(Func proceed, object self, Type type, bool isExplicit) { if (type.IsInterface) return proceed(); return null; } public override object BinaryOperation(Func proceed, object self, ExpressionType operation, object value) { switch (operation) { case ExpressionType.Equal: return ReferenceEquals(value, Nil.Instance) || value == null; case ExpressionType.NotEqual: return !ReferenceEquals(value, Nil.Instance) && value != null; } return proceed(); } } }