コード例 #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
ファイル: DeviceInstance.cs プロジェクト: vinc92/Beatrice
        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
ファイル: DeviceFeature.cs プロジェクト: vinc92/Beatrice
 public Task InvokeAsync(DeviceFeatureInvocationContext ctx, ActionCommand actionCommand)
 {
     return(DeviceFeatureHelper.InvokeAsync(this, ctx, actionCommand));
 }