用于从键/值对构建对象的库

时间:2013-03-06 19:01:38

标签: c#

IDictionary<string, string>的类编写另一个样板构建器后,我正在寻找一些以声明式样式执行它的库。这是我想要的一个例子。你能建议任何允许这样做的库吗?

class Person
{
    string Name;
    int Age;
}

class PersonBuilder
{
    public Person Build(IDictionary<string, string> values)
    {
        // MagicBuilder is what I'm looking for
        var magicBuilder = new MagicBuilder<Person>();
        // MagicBuilder.AddRequiredField<Type>(key, field)
        magicBuilder.AddRequiredField<string>("name", p => p.Name);
        // MagicBuilder.AddOptionalField<Type>(key, field, defaultValue)
        magicBuilder.AddOptionalField<int>("age", p => p.Age, 0);
        return magicBuilder.Build(values);
    }
}

0 个答案:

没有答案