向内部属性添加DataAnnotation验证

Jac*_*ack 5 c# data-annotations

是否可以验证具有数据注释的内部属性?

例如:

public class Customer
{
    [Required, Range(1, int.MaxValue)]
    internal int CustomerID { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

验证使用时

Validator.TryValidateObject(customerInstance, new ValidationContext(customerInstance), null, true);
Run Code Online (Sandbox Code Playgroud)

它不是在验证内部字段。.我假设这是因为“ System.ComponentModel.DataAnnotations”程序集对我的程序集内部一无所知。

我在客户类之前尝试使用以下语句将DataAnnotation程序集设置为“ Friend”程序集:

[assembly: InternalsVisibleTo("System.ComponentModel.DataAnnotations, PublicKey=0024000004.....")]
Run Code Online (Sandbox Code Playgroud)

但这似乎不起作用。

我将CustomerID设置为内部的原因是,程序集之外的任何事情都不需要了解。