慕哥9229398
进一步扩大答案。通过使用Microsoft Patterns&Practices企业库5验证模块,除了常规数据注释可用的方法之外,您还可以打开许多验证方法。using Microsoft.Practices.EnterpriseLibrary.Validation;using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;[HasSelfValidation]public partial class Category : ICategory{ [SelfValidation] public void Validate(ValidationResults validationResults) { if (this.Title === "Credo") { validationResults.AddResult( new ValidationResult( "Category title cannot be a veiled reference to a former cool 2000AD character.", this, null, null, null)); } validationResults.AddAllResults( ValidationFactory .CreateValidator<ICategory>() .Validate(this)); }}using System;using System.ComponentModel.DataAnnotations;using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;public interface ICategory{ int Id { get; set; } [Required] [StringLengthValidator(1, 50, MessageTemplate = "Category title should be a maximum of 50 characters in length.")] string Title { get; set; }}