遍历泛型类的集合属性 [英] Looping through Generic Class's collection property

查看:109
本文介绍了遍历泛型类的集合属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管已经发布了许多问题,但是似乎没有一个问题对我有帮助.

Although many questions have been posted, none seem to help me on my issue.

我已经开始了一个新的Generics/Reflection冒险,而我只是想让自己的语法和概念成为现实.

I've started a new Generics / Reflection adventure and I'm just trying to get my head around the syntax and concepts.

我有一个Generic类,该类具有X个属性,其中一个是集合,都可以正常工作,但是我在按属性名称从集合props中提取值时遇到问题.

I have a Generic class with X amount of properties and one being a collection, all is working fine but I'm having problems extracting the values from the collection props by property name.

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() : ""));
        }
    }
}

如您所见,我已经确定了属性Props,现在我想遍历该属性并按属性名称提取值.

As you can see I've pinpointed the property Props and now I want to loop through that and pull values by property name.

item.ToString()仅显示类属性的名称空间,而不显示值

item.ToString() just prints the namespace of the class property and not the value

希望你好心的人能指出我正确的方向吗?

Hoping you kind folk can point me in the right direction?

推荐答案

您可能希望递归地潜入" items属性.

You probably want to recursively "dive" into the items properties.

请注意,只有概念性的代码片段,不能保证它在我在此处编写时能正常工作:

Note, only conceptual code fragments, no guarantee that it works as I write it here:

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

要打印道具",请执行以下操作:

To print the "Props", do:

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

这篇关于遍历泛型类的集合属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆