序列化数据注释
我想将 DataAnnotations 存储在数据库中。如何通过反射(或其他方式)检索 DataAnnotation 的字符串表示形式?
示例
public class Product
{
[DisplayName("Price")]
[Required]
[RegularExpression(@"^\$?\d+(\.(\d{2}))?$")]
public decimal UnitPrice { get; set; }
}
结果可以是 XML 或 JSON 数据,只要它是字符串化的。
I would like to store DataAnnotations inside a database. How can I retrieve a string representation of a DataAnnotation by reflection (or by other means)?
Example
public class Product
{
[DisplayName("Price")]
[Required]
[RegularExpression(@"^\$?\d+(\.(\d{2}))?$")]
public decimal UnitPrice { get; set; }
}
Result could be XML or JSON data as long as it is stringified.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这与 retrieve-custom-attribute-parameter-values 非常相似,我会将其用作您的解决方案的基础
this is very similar to retrieve-custom-attribute-parameter-values, i'd use it as a basis for your solution
您最好编写自己的验证提供程序,然后以更方便的形式将验证规则存储在数据库中。解析字符串以尝试实例化属性似乎比必要的工作更多。 :)
示例验证提供程序: http://bradwilson.typepad.com/blog/2009/10/enterprise-library-validation-example-for-aspnet-mvc-2.html
You'd be better off writing your own validation provider, and then just store the validation rules in your database in a more convenient form. Parsing the strings to try to instantiate the attributes seems like more work than necessary. :)
Sample validation provider: http://bradwilson.typepad.com/blog/2009/10/enterprise-library-validation-example-for-aspnet-mvc-2.html