public class DoctorInfo
{
[DisplayName("form1[0].P1[0].Physician-Name[0]")]
public string ProviderFullName { get; set; }
}
DisplayName을 가지고 있으면 프로그래밍 방식으로 속성 이름 "ProviderFullName"을 얻을 수 있습니까?DisplayName을 사용하여 속성 이름을 검색하는 방법은 무엇입니까?
public class DoctorInfo
{
[DisplayName("form1[0].P1[0].Physician-Name[0]")]
public string ProviderFullName { get; set; }
}
DisplayName을 가지고 있으면 프로그래밍 방식으로 속성 이름 "ProviderFullName"을 얻을 수 있습니까?DisplayName을 사용하여 속성 이름을 검색하는 방법은 무엇입니까?
당신은 속성을 반복 할 수 및 사용자 정의 속성 -
PropertyInfo[] properties = typeof(DoctorInfo).GetProperties();
foreach (PropertyInfo prop in properties)
{
object[] attrs = prop.GetCustomAttributes(true);
foreach (object attr in attrs)
{
DisplayNameAttribute displayName = attr as DisplayNameAttribute;
if (displayName != null)
{
var attributeName = displayName.DisplayName; // check if this matches what you want
string propertyName = prop.Name; }
}
}
DisplayName
이 주어지면 get all the PropertyInfo members을 클래스에 넣은 다음 그 중 하나가 이라는 속성이 있는지 확인하기 위해 반복합니다. MemberInfo.GetCustomAttributes을 사용합니다.
DisplayName
이 일치하는 경우 MemberInfo.Name
property을 사용하여 이름을 다시 가져옵니다.
코드가 내가 원하는 것을 정확 .. 감사합니다. –