【发布时间】:2020-10-30 23:42:10
【问题描述】:
我有以下课程
public class ScanDetails
{
public Lavasoft Lavasoft { get; set; }
public STOPzilla STOPzilla { get; set; }
public Zillya Zillya { get; set; }
public VirusBlokAda VirusBlokAda { get; set; }
public TrendMicro TrendMicro { get; set; }
public SUPERAntiSpyware SUPERAntiSpyware { get; set; }
public NProtect nProtect { get; set; }
public NANOAV NANOAV { get; set; }
}
每个子属性都是这样的一个单独的类
public class Lavasoft
{
public int scan_time { get; set; }
public DateTime def_time { get; set; }
public int scan_result_i { get; set; }
public string threat_found { get; set; }
}
我正在尝试获取其 threat_found 属性 !=""
我尝试过遍历属性
foreach (var prop in report.scan_results.scan_details.GetType().GetProperties())
{
Console.WriteLine("{0} = {1}", prop.Name, prop.GetValue("threat_found", null));
}
但我不断收到以下异常 -> 对象与所需类型不匹配
【问题讨论】:
-
当对应的类似乎共享公共属性时,为什么要使用单独的属性?这些公共属性可以(应该!)在基类或接口中重新组合,然后您可以使用
List或任何基类/接口 -
@Cid 我已经根据onlinehelp.opswat.com/mdcloud/…
Report myresult = JsonConvert.DeserializeObject<Report>(responseBody);返回的JSON创建了类 -
而不是循环 GetProperties() 尝试 GetProperty("threat_found") 然后调用它
-
@user326608 但是
scandetails包含多个值。 -
@techno 查看答案。你也可以使用 linq 选择
标签: c# .net class properties ienumerable