Adding minimal version of property selector, using reflection

This commit is contained in:
Florian Zevedei
2024-01-20 19:22:26 +01:00
parent d0a6b74244
commit 161f191c43
4 changed files with 107 additions and 1 deletions
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using TelegramBotBase.Base;
@@ -123,6 +124,23 @@ namespace DemoBot.ActionManager.Actions
public static class GuidAction_Extensions
{
public static void AddGuidAction<TForm>(this ExternalActionManager manager, string method, Expression<Func<TForm, Guid>> propertySelector)
where TForm : FormBase
{
if (!typeof(FormBase).IsAssignableFrom(typeof(TForm)))
{
throw new ArgumentException($"{nameof(TForm)} argument must be a {nameof(FormBase)} type");
}
var newValue = Expression.Parameter(propertySelector.Body.Type);
var assign = Expression.Lambda<Action<TForm, Guid>>(Expression.Assign(propertySelector.Body, newValue), propertySelector.Parameters[0], newValue);
var setter = assign.Compile(true);
manager.Add(new GuidAction<TForm>(method, setter));
}
public static void AddGuidAction<TForm>(this ExternalActionManager manager, string method, Action<TForm, Guid> action)
where TForm : FormBase
{
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using System.Linq.Expressions;
using TelegramBotBase.Base;
using TelegramBotBase.DependencyInjection;
using TelegramBotBase.Form;
@@ -101,7 +102,22 @@ namespace DemoBot.ActionManager.Actions
public static class StartWithAction_Extensions
{
public static void AddStartsWithAction<TForm>(this ExternalActionManager manager, string method, Expression<Func<TForm, String>> propertySelector)
where TForm : FormBase
{
if (!typeof(FormBase).IsAssignableFrom(typeof(TForm)))
{
throw new ArgumentException($"{nameof(TForm)} argument must be a {nameof(FormBase)} type");
}
var newValue = Expression.Parameter(propertySelector.Body.Type);
var assign = Expression.Lambda<Action<TForm, String>>(Expression.Assign(propertySelector.Body, newValue), propertySelector.Parameters[0], newValue);
var setter = assign.Compile(true);
manager.Add(new StartWithAction<TForm>(method, setter));
}
public static void AddStartsWithAction<TForm>(this ExternalActionManager manager, string value, Action<TForm, String> setProperty)
where TForm : FormBase
{