2010-08-15 7 views

답변

5
Type type = typeof(Product); 
DisplayNameAttribute att = (DisplayNameAttribute)type.GetProperty("Name").GetCustomAttributes(typeof(DisplayNameAttribute), true).SingleOrDefault(); 

이 속성이 항상 존재하는 가정합니다. 그렇지 않을 경우 수정하십시오.

편집 : 제품이 기본 클래스를 사용 Type type = p.GetType(); 아닌 경우
이 값 string x = att.DisplayName;

를 얻을 수 있습니다.

+0

Product Name 속성의 DisplayName이 MetaData 클래스 (ProductMetaData)를 통해 제공되는 경우 어떻게됩니까? 편집 된 업데이트를 참조하십시오. – randomguy

+0

해당 속성의 클래스를 확인하고 지정된 유형에 대한 참조를 가져옵니다. 대신 해당 유형의 속성을 반사하십시오. – jwsample

+1

부분 클래스에 주석이 달린 속성이 있지만 customAttributes가 0 인 이유는 무엇입니까? – danihp

관련 문제