2015-09-28 55 views
2

雖然已經發布了很多問題,但似乎沒有人幫助我解決我的問題。循環訪問泛型類的集合屬性

我已經開始了一個新的Generics/Reflection冒險,我只是試圖讓我的頭在語法和概念。

我有一個Generic類與X數量的屬性和一個是一個集合,所有工作正常,但我有問題從屬性名稱提取集合props的值。

foreach (var property in typeof(T).GetProperties()) 
{ 
    if (property.Name == "Props") 
    { 
     foreach (var item in (IEnumerable)property.GetValue(type, null)) 
     { 
      var propertyName = ""; 
      var newValue = ""; 
      var oldValue = ""; 

      sbDescription.AppendLine(strDescriptionVals 
       .Replace("{0}", (item.ToString() == "PropertyName") ? item.ToString() : "" + ", ")); 

      sbAllNotes.AppendLine(strAllNotes 
       .Replace("{0}", (item.ToString() == "PropertyName") ? item.ToString() : "") 
       .Replace("{1}", (item.ToString() == "NewValue") ? item.ToString() : "") 
       .Replace("{2}", (item.ToString() == "OldValue") ? item.ToString() : "")); 
     } 
    } 
} 

正如你可以看到我已經精確定位屬性的道具,現在我通過希望環路和屬性名拉值。

item.ToString()只是打印類屬性的命名空間,而不是價值

希望你一種民間可以點我在正確的方向?

+0

什麼'type'變量? – Sybren

+0

@Sybren它的(T)屬性在每個方法調用上幾乎相同,這就是爲什麼它是通用的。 –

+0

這是一個非常「匿名」的功能和類的集合。問題在於僅從「集合屬性」中檢索值。 –

回答

2

你可能想要遞歸地「潛入」項目屬性。

注意,只是概念上的代碼片段,不能保證它的工作原理,我寫在這裏:

void Print(object value, string propertyName) 
{ 
    if (property.PropertyType == typeof(string) || property.PropertyType.IsPrimitive) 
    { 
     sbAllNotes.AppnedLine(.. propertyName .. value ..); 
    } 
    else if ((typeof(IEnumerable).IsAssignableFrom(property.PropertyType) 
    { 
     PrintCollectioType((IEnumerable)value); 
    } 
    else 
    { 
     PrintComplexType(value); 
    } 
} 

void PrintCollectioType(IEnumerable collection) 
{ 
    foreach (var item in collection) 
    { 
    Print(item, "Collection Item"); 
    } 
} 

void PrintComplexType(IEnumerable collection) 
{ 
    foreach(var property in owner.GetType().GetProperties()) 
    { 
    var propertyName = property.Name; 
    var propertyValue = property.GetValue(owner); 
    Print(item, propertyName); 
    } 
} 

要打印的 「道具」,這樣做:

var props = typeof(T).GetProperty("Props"); 
var propsValue = props.GetValue(rootObject); 
Print(propsValue, "Root"); 
1

的代碼打和理解邏輯多一點之後,它發生,我認爲是被人們譽爲「獲取的類型和屬性」關於Props迭代簡單:

 //Get the collection property 
     foreach (var property in typeof(T).GetProperties()) 
     { 
      if (property.Name == "Props") 
      { 
       foreach (var item in (IEnumerable)property.GetValue(type, null)) 
       { 
        //Because props is a collection, Getting the type and property on each pass was essential 
        var propertyName = item.GetType().GetProperty("PropertyName").GetValue(item, null); 
        var newValue = item.GetType().GetProperty("NewValue").GetValue(item, null); 
        var oldValue = item.GetType().GetProperty("OldValue").GetValue(item, null); 

        sbDescription.AppendLine(strDescriptionVals 
         .Replace("{0}", (propertyName != null) ? propertyName.ToString() : "" + ", ")); 

        sbAllNotes.AppendLine(strAllNotes 
         .Replace("{0}", (propertyName != null) ? propertyName.ToString() : "") 
         .Replace("{1}", (newValue != null) ? newValue.ToString() : "") 
         .Replace("{2}", (oldValue != null) ? oldValue.ToString() : "")); 
       } 
      } 
     }