本文介绍了我如何才能找到数据注解属性和使用反射及其参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些数据注解属性是这样的:
I have some data annotation attribute like this:
[StringLength(20, MinimumLength = 5, ErrorMessage = "First name must be between 5 and 20 characters")]我怎么能查找数据注解属性和使用反射及其参数?
How I can Find Data Annotation attributes and their parameters using reflection?
感谢
推荐答案我假设你有这样的事情:
I assume you have something like this:
[StringLength(20, MinimumLength = 5, ErrorMessage = "First name must be between 5 and 20 characters")] public string FirstName {get;set;}要从中获得属性和属性:
To get the attribute and a property from it:
StringLengthAttribute strLenAttr = typeof(Person).GetProperty("FirstName").GetCustomAttributes( typeof(StringLengthAttribute), false).Cast<StringLengthAttribute>().Single(); int maxLen = strLenAttribute.MaximumLength;更多推荐
我如何才能找到数据注解属性和使用反射及其参数
发布评论