在 lua 中,可以很容易地创建一个表并向其添加函数。
我如何在 C# 中做到这一点?我的第一个想法是使用委托列表并用匿名函数填充列表,但我无法正确使用语法。
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
}
}