Adding short non generic version

This commit is contained in:
Florian Zevedei 2024-01-22 03:58:00 +01:00
parent aee7562424
commit fc1e8e84c6
3 changed files with 34 additions and 11 deletions

View File

@ -152,6 +152,22 @@ namespace DemoBot.ActionManager.Actions
manager.Add(new GuidAction<TForm>(method, action));
}
public static void AddGuidAction(this ExternalActionManager manager, Type formType, string value, Expression<Func<FormBase, Guid>> 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<Action<FormBase, Guid>>(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<FormBase, Guid> action)
{
if (!typeof(FormBase).IsAssignableFrom(formType))

View File

@ -129,6 +129,22 @@ namespace DemoBot.ActionManager.Actions
manager.Add(new StartWithAction<TForm>(value, setProperty));
}
public static void AddStartsWithAction(this ExternalActionManager manager, Type formType, string value, Expression<Func<FormBase, String>> 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<Action<FormBase, String>>(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<FormBase, String> setProperty)
{
if (!typeof(FormBase).IsAssignableFrom(formType))

View File

@ -81,17 +81,8 @@ namespace DemoBot
//Waiting for input starting with 'a_'
config.AddStartsWithAction<HiddenForm>("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<HiddenTicketForm>("tickets", a => a.ticketId);