Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 向内部属性添加DataAnnotation验证_C#_Data Annotations - Fatal编程技术网

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设置为内部,是因为我的程序集之外不需要了解它