我試圖對未綁定到UI的對象執行一些驗證。例如,我有以下三類:企業庫對象驗證:獲取有關驗證失敗的更多信息
public class XDeftable {
[ObjectCollectionValidator(typeof(XSchedGroup))]
public List<XSchedGroup> SCHED_GROUP { get; set; }
}
[IdentifyingProperty("TABLE_NAME")]
public class XSchedGroup {
[ObjectCollectionValidator(typeof(XJob))]
public List<XJob> JOB { get; set; }
[Required]
public string TABLE_NAME { get; set; }
}
[IdentifyingProperty("JOBNAME")]
public class XJob : ICalendar {
[Required]
public string JOBNAME { get; set; }
[Range(-62, 62)]
public string SHIFTNUM { get; set; }
[ObjectCollectionValidator(typeof(XTagNames))]
public List<XTagNames> TAG_NAMES { get; set; }
}
XDeftable - > XSchedGroup - > XJob - > XTagNames
當一個對象未通過驗證工作的事情完全按照人們所期望的,但如果我只是檢查的ValidationResult因爲它是密鑰和消息我最終得到了類似於:「JOBNAME | Field is required。」
的問題,這是考慮到我可能有數百個就業機會在一個調度組的驗證是無用的,因爲我不知道哪一個特定作業失敗做。我搜索了關於驗證和C#的各種文檔,並且沒有找到獲取更多數據的方法。我創建了屬性IdentifyingProperty以允許我標記該類的哪個屬性標識了該類的特定實例。我有一個基於Git Repo:https://github.com/reustmd/DataAnnotationsValidatorRecursive/tree/master/DataAnnotationsValidator/DataAnnotationsValidator的以前的自定義驗證解決方案。它工作正常,但我想交換更強大的東西。
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public sealed class IdentifyingProperty : Attribute {
public string Name { get; set; }
public IdentifyingProperty(string name) {
this.Name = name;
}
}
到目前爲止,我已經能夠想出如下:
這至少會回到我「XJob [作業名] .SHIFTNUM |現場SHIFTNUM必須介於 - 62和62.「我仍然喜歡它,如果有一種方法可以讓我獲得跟隨容器鏈的結果,例如:XSchedGroup [TABLE_NAME] .XJob [JOBNAME] .SHIFTNUM。
請注意,這不是特定於C#編程語言:
所以你的方法是這樣堅持。 –
@JohnSaunders編輯標籤到.Net。 – Kittoes0124
非常好。我已經改變了標題。 –