private static void Send(IConcurrentObject @object, string name, bool wait, object[] args) { var method = @object .GetType() .GetMethods() .Where(m => m.Name == name && m .ReturnType .ToString() .Contains("Task")) .SingleOrDefault(); if (method != null) { args = args.Union(new object[] { default(CancellationToken) }).ToArray(); var tsk = method.Invoke(@object, args); if (wait) { ((Task)tsk).Wait(); } } else { var property = @object .GetType() .GetProperty(name); Debug.Assert(property != null && property.CanWrite); property.SetValue(@object, args[0]); } }
private static void RunStep(IConcurrentObject @object, string methodName, Func <string> moveNext, Action <Exception> failure) { var method = @object.GetType().GetMethod(methodName, new[] { typeof(CancellationToken), typeof(Action <object>), typeof(Action <Exception>), }); Action <object> success = (res) => { try { var next = moveNext(); if (next != null) { RunStep(@object, next, moveNext, failure); } } catch (Exception ex) { failure(ex); } }; method.Invoke(@object, new object[] { default(CancellationToken), success, failure }); }