如何从带有反射的嵌套列表中获取价值?

时间:2019-02-06 07:17:58

标签: c# linq reflection

我有一个名为filterTarget的嵌套列表。

有关内容的示例:

filterTarget = { Id: 1
InventoryDescription: {Type: "Machine", Code: "1/1", Description: "1", Category: "1", StockCode: "1", …}
InventoryRecordDetails: []
Length: "1"
Life: "04.02.2019"
Location: null
Model: "1" }

该示例我想获取InventoryDe​​scription代码的值。

我为两层编写了这段代码,但是看起来很脏而且不够用。这不是一般的。

filterTarget = filterTarget
          .Where(x =>  x.GetType()  
                        .GetProperty(fieldval1)
                        .PropertyType
                        .GetProperty(fieldval2)
                        .GetValue(x.GetType()
                                   .GetProperty(fieldval1)
                                   .GetValue(x))
                        .ToString()?
                        .ToLower() == deger)
          .ToList();

如何从带有反射和linq的嵌套列表中获取价值?

2 个答案:

答案 0 :(得分:0)

如果您正在寻找linq解决方案,那么let将是您的朋友:

filterTarget = (from f in filterTarget
                let temp = f.GetType().GetProperty(fieldval1).GetValue(f)
                let temp2 = temp.GetType().GetProperty(fieldval2).GetValue(temp)
                where temp2.ToString().ToLower() == deger
                select f).ToList();

但过时的我觉得它不太可读。具有适当if条件的普通循环将更易于维护:

public IEnumerable<T> FilterMyList<T>(IEnumerable<T> list, string fieldval1, string fieldval2, string deger)
{
    foreach (var element in list)
    {
        var nesteValue1 = element.GetType().GetProperty(fieldval1).GetValue(element);
        var nestedValue2 = nesteValue1.GetType().GetProperty(fieldval2).GetValue(nesteValue1);
        if (nestedValue2.ToString().ToLower() == deger)
        {
            yield return element;
        }
    }
}

这是使用递归深入嵌套对象的更通用的方法。您需要提供确切数量的属性名称,并且它必须与嵌套级别匹配:

public IEnumerable<T> FilterMyListRecursive<T>(IEnumerable<T> list, string searchpattern, params string [] propertynames) where T: class
{
    foreach (var element in list)
    {       
        if (TryNestedRecursiveDescend(element, searchpattern, propertynames))
        {
            yield return element;
        }       
    }
}

private bool TryNestedRecursiveDescend<T>(T obj, string searchpattern, params string [] propertynames) where T: class
{   
    var nestedValue = obj.GetType().GetProperty(propertynames.First()).GetValue(obj);
    // if you are at the lowest level that you can check
    if (fieldvalues.Length == 1)
    {       
        return nestedValue.ToString().ToLower() == searchpattern;
    }
    else
    {
        // recursive call with the remaining propertynames
        return TryNestedDescend(nestedValue, searchpattern, propertynames.Skip(1).ToArray());
    }
}

答案 1 :(得分:0)

filterTarget是我的嵌套列表。

字段是有序属性字符串。

  filterTarget = (from f in filterTarget
  let temp = GetPropertyValue(f, Field)
  where temp?.ToString().ToLower() == "deger"
  select f).ToList();

  private static object GetPropertyValue<T>(T filterTarget, string field)
  {
   string[] fields = field.Split('.');
   object value = filterTarget;
   fields?.ToList().ForEach(p =>
   {
      value = value.GetType().GetProperty(p).GetValue(value);
   });

    return value;
  }