我正在使用NRules,尝试从数据库加载规则。为此,我必须使用反射来生成表达式。
我的目标是在运行时生成"
我可以通过使用反射来生成Action:
public class Product
{
public string Attribute1 { get; }
public List<int> Category { get; set; }
public void AddCategory (int category){
this.Category.Add(category);
}
}
using NRules.RuleModel;
using NRules.RuleModel.Builders;
var builder = new RuleBuilder();
//some logic for buildin lefthand side
Expression<Action<IContext, Product>> action = (ctx, product) => product.AddCategory(25);
builder.RightHandSide().Action(action);
我的目标是在运行时生成"
Expression<Action<IContext, Product>> action = (ctx, product) => product.AddCategory(25);
"。我认为使用反射是实现这一目标的唯一方法,因为我需要从数据库中读取某些值。我可以通过使用反射来生成Action:
Type actionType = typeof(Action<>).MakeGenericType(new Type[] { typeof(IContext),
Type.GetType(actualModelName) });
MethodInfo eventMethodInfo = type.GetMethod("AddCategory");
Action actionFromReflection = (Action)Delegate.CreateDelegate(actionType, eventMethodInfo);
但是NRules方法需要以LambdaExpression作为参数。
我该如何将“actionFromReflection”转换为LambdaExpression?
LambdaExpression le = actionFromReflection ???