繁体   English   中英

序列化DataAnnotations

[英]Serializing DataAnnotations

我想将DataAnnotations存储在数据库中。 如何通过反射(或其他方法)检索DataAnnotation的字符串表示?

public class Product
    {
        [DisplayName("Price")]
        [Required]
        [RegularExpression(@"^\$?\d+(\.(\d{2}))?$")]
        public decimal UnitPrice { get; set; }
    }

结果可以是XML或JSON数据,只要它是字符串化的。

这与retrieve-custom-attribute-parameter-values非常相似,我将它用作解决方案的基础

您最好编写自己的验证提供程序,然后以更方便的形式将验证规则存储在数据库中。 解析字符串以尝试实例化属性似乎比必要的工作更多。 :)

样本验证提供者: http//bradwilson.typepad.com/blog/2009/10/enterprise-library-validation-example-for-aspnet-mvc-2.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM