遍历通用 Class 的集合 属性

Looping through Generic Class's collection property

尽管已经发布了很多问题,none 似乎对我的问题有所帮助。

我开始了新的 Generics / Reflection 冒险,我只是想了解语法和概念。

我有一个 Generic class,其中有 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() 只打印 class 属性 的命名空间而不是值

希望好心人能给我指明正确的方向?

在玩代码并进一步理解逻辑之后,我想到这就像“获取类型和 属性”一样简单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() : ""));
                }
            }
        } 

您可能想要递归 "dive" 进入项目属性。

注意,只有概念性的代码片段,不能保证它像我在这里写的那样有效:

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);
  }
}

要打印 "Props",请执行:

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