In lua, one can very easily create a table and add functions to it.
How do I do that in C#? My first idea was to use a List of delegates and fill the list with anonymous functions, but I can’t get the syntax right.
public class PlanItem
{
public string Name { get; set; }
public List<BusinessRule> Rules;
}
public delegate bool BusinessRule(PlanItem item);
[Test]
public void TestIt()
{
PlanItem item = new PlanItem();
item.Rules.Add(
BusinessRule(PlanItem item)
{
return !string.IsNullOrEmpty(item.Name);
}
);
foreach(BusinessRule rule in item.Rules)
{
if(!rule(item))
// write uh-oh
}
}
This should do the trick: