Show The Code
using NRules;
using NRules.Fluent;
using System;
using System.Collections.Generic;
using System.Reflection;
/// <summary>
/// 规则引擎帮助类
/// </summary>
public class RuleHepler
{
public static List<dynamic> RunRules(List<string> assemList, List<dynamic> ruleList)
{
///加载规则
var repos = new RuleRepository();
if(null== assemList || assemList.Count <= 0)
{
throw new Exception($"{nameof(assemList)} must not null");
}
//加载动态程序集规则
var assemLoads = new List<Assembly>();
assemList.ForEach(assem=> {
var curAssem = Assembly.Load(assem);
assemLoads.Add(curAssem);
});
repos.Load(x => x.From(assemLoads));
///编译规则
var factory = repos.Compile();
///创建活动会话
var session = factory.CreateSession();
///将实体放入活动会话
foreach(var obj in ruleList)
{
session.Insert(obj);
}
///开启规则引擎处理
session.Fire();
return ruleList;
}
}