ArrayPropAssignmentBehavior.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace ClaySharp.Behaviors {
  5. public class ArrayPropAssignmentBehavior : ClayBehavior {
  6. public override object InvokeMember(Func<object> proceed, object self, string name, INamedEnumerable<object> args) {
  7. return
  8. IfSingleArray(args, arr => { ((dynamic)self)[name] = arr; return self; }, () =>
  9. IfTwoOrMoreArgs(args, arr => { ((dynamic)self)[name] = arr; return self; },
  10. proceed));
  11. }
  12. private object IfTwoOrMoreArgs(IEnumerable<object> args, Func<dynamic, object> func, Func<object> proceed) {
  13. if (args.Count() < 2)
  14. return proceed();
  15. return func(NewArray().AddRange(args));
  16. }
  17. private object IfSingleArray(IEnumerable<object> args, Func<dynamic, object> func, Func<object> proceed) {
  18. if (args.Count() != 1)
  19. return proceed();
  20. var arr = args.Single();
  21. if (arr == null)
  22. return proceed();
  23. if (!typeof(Array).IsAssignableFrom(arr.GetType()))
  24. return proceed();
  25. return func(NewArray().AddRange(arr));
  26. }
  27. private static dynamic NewArray() {
  28. return new Clay(
  29. new InterfaceProxyBehavior(),
  30. new PropBehavior(),
  31. new ArrayPropAssignmentBehavior(),
  32. new ArrayBehavior(),
  33. new NilResultBehavior());
  34. }
  35. }
  36. }