如何使用reflection查找数据注释属性及其参数
我有一些数据注释属性,如下所示:
[StringLength(20, MinimumLength = 5, ErrorMessage = "First name must be between 5 and 20 characters")]
如何使用reflection查找数据注释属性及其参数?
谢谢
我假设你有这样的事情:
[StringLength(20, MinimumLength = 5, ErrorMessage = "First name must be between 5 and 20 characters")] public string FirstName {get;set;}
要从中获取属性和属性:
StringLengthAttribute strLenAttr = typeof(Person).GetProperty("FirstName").GetCustomAttributes( typeof(StringLengthAttribute), false).Cast().Single(); int maxLen = strLenAttribute.MaximumLength;