C# 向内部属性添加DataAnnotation验证
是否可以验证具有数据批注的内部属性 例如:C# 向内部属性添加DataAnnotation验证,c#,data-annotations,C#,Data Annotations,是否可以验证具有数据批注的内部属性 例如: public class Customer { [Required, Range(1, int.MaxValue)] internal int CustomerID { get; set; } } 验证时使用 Validator.TryValidateObject(customerInstance, new ValidationContext(customerInstance), null, true); 它不是在验证内部字段。。我
public class Customer
{
[Required, Range(1, int.MaxValue)]
internal int CustomerID { get; set; }
}
验证时使用
Validator.TryValidateObject(customerInstance, new ValidationContext(customerInstance), null, true);
它不是在验证内部字段。。我假设这是因为“System.ComponentModel.DataAnnotations
”程序集对我的程序集的内部内容一无所知
在我的Customer类之前,我尝试使用以下语句将DataAnnotation程序集设置为“Friend”程序集:
[assembly: InternalsVisibleTo("System.ComponentModel.DataAnnotations, PublicKey=0024000004.....")]
但这似乎不起作用
我之所以将CustomerID设置为内部,是因为我的程序集之外不需要了解它