From fc1e8e84c6c45bbcb5360b5d7e4e88c42a941de2 Mon Sep 17 00:00:00 2001 From: Florian Zevedei Date: Mon, 22 Jan 2024 03:58:00 +0100 Subject: [PATCH] Adding short non generic version --- .../DemoBot/ActionManager/Actions/GuidAction.cs | 16 ++++++++++++++++ .../ActionManager/Actions/StartWithAction.cs | 16 ++++++++++++++++ .../ExternalActionManager/DemoBot/Program.cs | 13 ++----------- 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/Experiments/ExternalActionManager/DemoBot/ActionManager/Actions/GuidAction.cs b/Experiments/ExternalActionManager/DemoBot/ActionManager/Actions/GuidAction.cs index 1f2bdeb..fc593b9 100644 --- a/Experiments/ExternalActionManager/DemoBot/ActionManager/Actions/GuidAction.cs +++ b/Experiments/ExternalActionManager/DemoBot/ActionManager/Actions/GuidAction.cs @@ -152,6 +152,22 @@ namespace DemoBot.ActionManager.Actions manager.Add(new GuidAction(method, action)); } + public static void AddGuidAction(this ExternalActionManager manager, Type formType, string value, Expression> propertySelector) + { + if (!typeof(FormBase).IsAssignableFrom(formType)) + { + throw new ArgumentException($"{nameof(formType)} argument must be a {nameof(FormBase)} type"); + } + + var newValue = Expression.Parameter(propertySelector.Body.Type); + + var assign = Expression.Lambda>(Expression.Assign(propertySelector.Body, newValue), propertySelector.Parameters[0], newValue); + + var setter = assign.Compile(true); + + manager.Add(new GuidAction(formType, value, setter)); + } + public static void AddGuidAction(this ExternalActionManager manager, Type formType, string method, Action action) { if (!typeof(FormBase).IsAssignableFrom(formType)) diff --git a/Experiments/ExternalActionManager/DemoBot/ActionManager/Actions/StartWithAction.cs b/Experiments/ExternalActionManager/DemoBot/ActionManager/Actions/StartWithAction.cs index 4e17136..2c46f63 100644 --- a/Experiments/ExternalActionManager/DemoBot/ActionManager/Actions/StartWithAction.cs +++ b/Experiments/ExternalActionManager/DemoBot/ActionManager/Actions/StartWithAction.cs @@ -129,6 +129,22 @@ namespace DemoBot.ActionManager.Actions manager.Add(new StartWithAction(value, setProperty)); } + public static void AddStartsWithAction(this ExternalActionManager manager, Type formType, string value, Expression> propertySelector) + { + if (!typeof(FormBase).IsAssignableFrom(formType)) + { + throw new ArgumentException($"{nameof(formType)} argument must be a {nameof(FormBase)} type"); + } + + var newValue = Expression.Parameter(propertySelector.Body.Type); + + var assign = Expression.Lambda>(Expression.Assign(propertySelector.Body, newValue), propertySelector.Parameters[0], newValue); + + var setter = assign.Compile(true); + + manager.Add(new StartWithAction(formType, value, setter)); + } + public static void AddStartsWithAction(this ExternalActionManager manager, Type formType, string value, Action setProperty) { if (!typeof(FormBase).IsAssignableFrom(formType)) diff --git a/Experiments/ExternalActionManager/DemoBot/Program.cs b/Experiments/ExternalActionManager/DemoBot/Program.cs index e739f49..e108f98 100644 --- a/Experiments/ExternalActionManager/DemoBot/Program.cs +++ b/Experiments/ExternalActionManager/DemoBot/Program.cs @@ -81,17 +81,8 @@ namespace DemoBot //Waiting for input starting with 'a_' config.AddStartsWithAction("a_", a => a.value); - - //Waiting for input starting with 't_' - config.AddStartsWithAction(typeof(HiddenForm), "t_", (a, b) => - { - var hf = a as HiddenForm; - if (hf == null) - return; - - hf.value = b; - }); - + //Waiting for input starting with 't_' (Non generic version) + config.AddStartsWithAction(typeof(HiddenForm), "t_", a => ((HiddenForm)a).value); //Deserialize input and waiting for the method property to has value 'tickets' config.AddGuidAction("tickets", a => a.ticketId);