我正在使用NRules,并尝试从数据库中加载规则。
为此,我必须使用反射来生成表达式。
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) => product.AddCategory(25);
”。我认为使用反射的唯一方法是反射。因为我正在从数据库中读取一些值。
我可以通过反射来生成动作:
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 ???