你需要用
SetsRequiredMembersAttribute
。
否则,您必须设置
required
对象初始值设定项中的属性。
using System.Diagnostics.CodeAnalysis;
public class PTInspectionAndTMDefectsDTO
{
public required PTINSPECTION PtInspection { get; set; }
public IEnumerable<TMDEFECT> TMDefects { get; set; } = Enumerable.Empty<TMDEFECT>();
// Constructors.
[SetsRequiredMembers]
public PTInspectionAndTMDefectsDTO(PTINSPECTION ptinspection)
{
PtInspection = ptinspection;
}
[SetsRequiredMembers]
public PTInspectionAndTMDefectsDTO(PTINSPECTION ptinspection, IEnumerable<TMDEFECT> tmdefects)
{
PtInspection = ptinspection;
TMDefects = tmdefects;
}
}
请注意:
是否应该有一个不设置所有
必需
属性则不使用标记该ctor
[SetsRequiredMembers]
。