2012-09-04 4 views
2

속성이 적용된 속성이 있습니다. 속성 getter 또는 setter 내부에서 속성에 액세스하려면 어떻게해야합니까?속성 내부의 액세스 속성

class Person { 

    [ID("A","B")] 
    public Address HomeAddress 
    { 
     get 
     { 

      // Get values A and B here ? 

     } 
     set { } 
    } 
} 

정말 어떻게되는지 알지 못합니다.

감사

+2

왜 당신이 어떻게해야합니까이 코드를 시도 할 수 있습니다? –

+0

http://stackoverflow.com/questions/4689361/how-to-get-attribute-on-property-in-property-get-or-set-body이 질문을 참조하십시오 –

+0

달성하려는 것은 무엇입니까? – DarthVader

답변

2
class Person { 

    [ID("A","B")] 
    public Address HomeAddress 
    { 
     get 
     { 
      System.Attribute[] attrs = System.Attribute.GetCustomAttributes(Person); 
      // use attrs[0] to get "A"; 
      // use attrs[1] to get "B"; 
     } 
     set { } 
    } 
} 

시는 : 난 그냥 여기서 직접 쓴, 지금은 비주얼 스튜디오를 사용하지 않는. 사소한 오류가 발생하면 죄송합니다.


this MSDN article을 살펴보십시오.

System.Attribute[] attrs = System.Attribute.GetCustomAttributes(Person); // Reflection. 

// Displaying output. 
foreach (System.Attribute attr in attrs) 
{ 
    System.Console.WriteLine(attr); 
} 
0

당신은

PropertyInfo[] propertyInfo = type.GetProperties(); 
foreach (var m in propertyInfo) 
{      

     foreach (Attribute a in m.GetCustomAttributes(true)) 
     { 

     } 
}