2015-09-28 58 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() : "")); 
       } 
      } 
     }