2010-06-24 17 views
5

Bir PropertyInfo, İşte kodunu almak istiyorum:nasıl iç içe almak için özellikler

string propertyName="Text"; 
PropertyInfo pi = control.GetType().GetProperty(propertyName); 

iyi çalışıyor ama iç içe özelliklerini almak istiyorsanız, boş döndürür:

string propertyName="DisplayLayout.Override.RowSelectors"; 
PropertyInfo pi = control.GetType().GetProperty(propertyName); 

mi iç içe geçmiş özellikleri almak için herhangi bir yolu var mı?

Saygılarımızla,

Florian

Düzenleme:

string propertyName="DisplayLayout.Bands[0].Columns"; 
PropertyInfo pi = control.GetType().GetProperty(propertyName) 

size

cevap

7

Evet:

public PropertyInfo GetProp(Type baseType, string propertyName) 
{ 
    string[] parts = propertyName.Split('.'); 

    return (parts.Length > 1) 
     ? GetProp(baseType.GetProperty(parts[0]).PropertyType, parts.Skip(1).Aggregate((a,i) => a + "." + i)) 
     : baseType.GetProperty(propertyName); 
} 

Aranan: kazanmak için

PropertyInfo pi = GetProp(control.GetType(), "DisplayLayout.Override.RowSelectors"); 

Özyineleme!

+0

Teşekkür ederiz! Bir endeks aracılığıyla bir özellik nasıl elde edeceğinizi öğrenmek istiyorum: string propertyName = "DisplayLayout.Bands [0] .Columns"; PropertyInfo pi = control.GetType() GetProperty (propertyName); – Florian

3

Sadece ederiz: Ben bir dizidir bir özellik almak istiyorum, şimdi yeni bir sorun var PropertyType'da tekrar aynısını yapın (sadece ihtiyaç duyduğunuz sıklıkta tekrarlayın):

PropertyInfo property = GetType().GetProperty(propertyName); 
PropertyInfo nestedProperty = property.PropertyType.GetProperty(nestedPropertyName) 
+0

Evet, cevabımdaki PropertyType bitini unuttum. –

0

Bunu yapabilirsin, ama yani her seviye için "her şeyi" yapmak zorunda:

  • senin nesne türü
  • gelen mülkü al o mülk türünü alın
  • durulayın ve tekrar :)