2011-10-23 9 views

Trả lời

12

Tôi giả sử bạn có một cái gì đó như thế này:

[StringLength(20, MinimumLength = 5, ErrorMessage = "First name must be between 5 and 20 characters")] 
public string FirstName {get;set;} 

Để có được thuộc tính và một tài sản từ nó:

StringLengthAttribute strLenAttr = 
    typeof(Person).GetProperty("FirstName").GetCustomAttributes(
    typeof(StringLengthAttribute), false).Cast<StringLengthAttribute>().Single(); 


int maxLen = strLenAttribute.MaximumLength;