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

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

问题描述

虽然已经发布了很多问题,但在我的问题上似乎没有任何帮助。



我开始了一个新的泛型 / Reflection 冒险,我只是试图让我的头在语法和概念。



我有一个 Generic 类,其中X属性的数量和一个集合,所有工作正常,但我有问题提取集合中的值<$ c $ (T).GetProperties(var)属性中的var属性。

通过属性名称 props 。

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



code
$ b

正如你所看到的, ve精确定位了属性Props,现在我想循环并通过属性名称拉取值。

$ c>只是打印类属性的名称空间而不是值

希望你们善良的人可以指引我朝着正确的方向吗?



请注意,只有概念性代码才可以使用。

解决方案

碎片,不保证它在我这里写的时候工作:

  void打印(对象值,字符串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);
}
}

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

PrintComplexType(IEnumerable collection)
$ b foreach(owner.GetType()中的var属性)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);


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

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

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

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() just prints the namespace of the class property and not the value

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

解决方案

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天全站免登陆