【问题标题】:How do I get the List values of a property of a class?如何获取类属性的列表值?
【发布时间】:2019-10-29 16:41:02
【问题描述】:

我正在尝试遍历类的属性。 它工作正常,直到我得到一个定义为列表的属性。 有没有一种简单的方法来处理这个问题,因为我很确定,但是 不幸的是,我无法找到解决方案。

当我遍历属性时,将显示特定类的正确值,但只有当它是单个值时,我才能获得任何列表值。 如果我使用属性迭代,属性列表将显示为

System.Collections.Generic.List`1[System.Double]。

关于我的程序的一些背景知识。我动态地为我的类创建了一个List,它具有不同的属性值,因此为正确的类获取正确的值很重要。

...

for(int j = 0; j<intensityList.Count; j++) {

                getIntensityofMode(intensityList[j]);
                Report.Log(ReportLevel.Info, "");
                Console.ReadLine();
            }
        }
        public void getIntensityofMode(HelpingClasses.IntensityData mode) {

            Type type = typeof(HelpingClasses.IntensityData);
            PropertyInfo[] properties = type.GetProperties();
            foreach (PropertyInfo property in properties)
            {
                if(property.GetValue(mode, null) != null) {
                    Report.Log(ReportLevel.Info, property.Name + " = " + property.GetValue(mode, null));

                    if(property.PropertyType.IsGenericType &&
                       property.PropertyType.GetGenericTypeDefinition() == typeof(List<>)) {
                        Report.Log(ReportLevel.Info,"its a list");

                        foreach() 
//iterate through the property which is a list and holds several values
                        {
                        }
                    }
                } else {
                    Report.Log(ReportLevel.Info, property.Name + " = NaN");
                }
            }

所以我想,我只需要在foreach 循环中写一行就可以得到我想要得到的东西,但我不知道如何处理它。

//variables
        public string TxVoltagePulseMode{ get; set; }
        public double TxVoltagePulseDB{ get; set; }
        public double TxVoltagePulseVoltage{ get; set; }
        public List<double> TxFocusdepth{ get; set; }

所以除了TxFocusdepth之外,所有代码都可以使用。

【问题讨论】:

  • 检查属性是否为集合。如果是,则将该值提供给 for 或 foreach 循环。
  • 是的,我知道步骤,但我不知道如何实现它..
  • 因为我已经检查了它是否是一个列表,但我没有得到列表的值

标签: c# list loops foreach properties


【解决方案1】:

您可以完全忽略它是一个泛型列表这一事实,而只是将其视为非泛型 IList。

...

for(int j = 0; j<intensityList.Count; j++) {

            getIntensityofMode(intensityList[j]);
            Report.Log(ReportLevel.Info, "");
            Console.ReadLine();
        }
    }

public void getIntensityofMode(HelpingClasses.IntensityData mode) {

    Type type = typeof(HelpingClasses.IntensityData);
    PropertyInfo[] properties = type.GetProperties();
    foreach (PropertyInfo property in properties)
    {
        if(property.GetValue(mode, null) != null) {
            var value = property.GetValue(mode, null);
            // Test if it is IList and cast it if so
            if (value is IList e) {
                // Print the value of elements in the enumerable list
                foreach (var v in e) {
                    Report.Log(ReportLevel.Info, property.Name + " = " + v.ToString());
                }
            } else {
               Report.Log(ReportLevel.Info, property.Name + " = " + value);
            }
        }
    }
}

【讨论】:

  • 嘿,感谢您的快速答复。不幸的是它不起作用,它只返回单个值,但返回列表属性值。所以我的输出是 TxVoltagePulseMode = A TxVoltagePulseDB = 0 TxVoltagePulseVoltage = 4.731 但它不会返回列表,尽管它必须向其他人提及,但我只能在 c# 7 或更高版本中使用 IEnumerable。因为我使用的是 ranorex,它一开始使用的是 c# 5,所以一开始它没有用
  • 好吧,对不起,列表一直是空的。但问题仍然存在,它说: System.Collections.Generic.List`1[System.Double] 但是 4 次,值的数量到底是多少,所以上面的解决方案不起作用
  • 嘿,我找到了解决方案!感谢上面的代码一直有效。它的 v.toString() 因为你想要 var v 的值而不是值。这就是为什么它不起作用。你可以编辑你的答案吗?我会在右边标记它,非常感谢
  • 也是它的 IList 而不是 ienumerable 因为它是 ienumerable ,它也会将字符串中的每个字符作为单个值返回,但是使用 IList,它会检查该值是否是 IList 然后它将返回列表的值
  • @MinhTV 编辑已获批准。感谢您的跟进。
猜你喜欢
  • 2010-10-18
  • 2020-02-29
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
  • 2014-02-27
  • 1970-01-01
  • 2021-03-26
相关资源
最近更新 更多