예제 #1
0
        public static Task InvokeAsync(DeviceFeature deviceFeature, DeviceFeatureInvocationContext ctx, ActionCommand actionCommand)
        {
            var actionCommandType = actionCommand.GetType();

            var invoker = _invokerCache.GetOrAdd(actionCommandType, _ =>
            {
                var actionCommandSupportType = typeof(IActionCommandSupport <>).MakeGenericType(actionCommandType);

                var method  = actionCommandSupportType.GetMethod("InvokeAsync");
                var thisArg = Expression.Parameter(typeof(object));
                var arg0    = Expression.Parameter(typeof(DeviceFeatureInvocationContext));
                var arg1    = Expression.Parameter(typeof(ActionCommand));

                // (thisArg, arg0, arg1) => (thisArg is IActionCommandSupport<T>)
                //     ? ((IActionCommandSupport<T>)thisArg).InvokeAsync(arg0, (<ActionCommand>)arg1)
                //     : Task.CompletedTask;
                var call = Expression.Condition(Expression.TypeIs(thisArg, actionCommandSupportType),
                                                Expression.Call(Expression.Convert(thisArg, actionCommandSupportType), method, arg0, Expression.Convert(arg1, actionCommandType)),
                                                Expression.Property(null, typeof(Task).GetProperty("CompletedTask")));
                var lambda = Expression.Lambda(call, thisArg, arg0, arg1);

                return((Func <object, DeviceFeatureInvocationContext, ActionCommand, Task>)lambda.Compile());
            });

            return(invoker(deviceFeature.Instance, ctx, actionCommand));
        }
예제 #2
0
        public Task InvokeAsync(ActionCommand actionCommand)
        {
            if (actionCommand == null)
            {
                return(Task.CompletedTask);
            }

            if (ActionCommand.ByType.TryGetValue(actionCommand.GetType(), out var command))
            {
                if (FeatureByCommand.TryGetValue(command, out var feature))
                {
                    var ctx = new DeviceFeatureInvocationContext(this);
                    return(feature.InvokeAsync(ctx, actionCommand));
                }
            }

            return(Task.CompletedTask);
        }
예제 #3
0
 public Task InvokeAsync(DeviceFeatureInvocationContext ctx, ActionCommand actionCommand)
 {
     return(DeviceFeatureHelper.InvokeAsync(this, ctx, actionCommand));
 }