I'm developing a sort of a rules engine, where users define a rule as a set of conditions and actions. Those conditions and actions are then parsed into code to be executed. I am able to generate the code without any issue. I'm stuck at the point of compiling it and then loading the class.
How do I take a string of dynamically generated source code and compile it at runtime?
How can I then execute that code?
I envision being able to have a static list of rules that would be updated as rules are added. Something like:
static Dictionary<string, Rule> Rules { get; set; }
static void RefreshRules ()
{
var newRules = DataLayer.GetRules().Where(r => !this.Rules.ContainsKey(r.Name));
foreach (var rule in newRules)
{
this.Rules[rule.Name] = CompileRule(rule.Code);
}
}
Or would I re-compile an assembly and then reload it into my already running app?