C# 如何用受保护的构造函数和私有属性设置填充对象?
我有一个域对象,如下所示:C# 如何用受保护的构造函数和私有属性设置填充对象?,c#,design-patterns,C#,Design Patterns,我有一个域对象,如下所示: public class Document { public string Index { get; private set; } public string Title { get; private set; } public string Content { get; private set; } public Document (string index, string title, string content) {
public class Document
{
public string Index { get; private set; }
public string Title { get; private set; }
public string Content { get; private set; }
public Document (string index, string title, string content)
{
// some domain rule here
Index = index;
Title = title;
Content = content;
}
protected Document() {}
}
现在,我想构造Document
类型的对象,并在我从存储器中提取数据时填充它们的属性,而不需要像Dapper这样的ORM
我想知道Dapper或JSONvert如何使用受保护的构造函数和私有属性设置器来填充对象?填充字段的构造函数不受保护,它是公共的 有些包足够聪明,或者带有属性,因此您可以教它们使用这些构造函数,而有些包只使用反射。可以使用反射写入私有属性