【问题标题】:Iterating through properties of a class and creation of a simple Object consolidating information遍历类的属性并创建一个简单的对象合并信息
【发布时间】: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


【解决方案1】:

你的问题在这里:

prop.GetValue("threat_found", null)

您正试图从索引为 null 的字符串“threat_found”中获取“Lavasoft”属性。

这行得通:

        foreach (var prop in report.scan_results.scan_details.GetType().GetProperties())
        {
            var threatFoundPropOfCurrentType = prop.PropertyType.GetProperty("threat_found");
            var threatFoundValueOfCurrentType = threatFoundPropOfCurrentType.GetValue(prop.GetValue(report.scan_results.scan_details)) as string;
            Console.WriteLine($"{prop.Name} = {threatFoundValueOfCurrentType}");
        }

【讨论】:

【解决方案2】:

试试这个:

public class ScanDetail
{
    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; }
}

public class ScanDetails
 {
    public ScanDetail Lavasoft { get; set; }
    public ScanDetail STOPzilla { get; set; }
    public ScanDetail Zillya { get; set; }
    public ScanDetail VirusBlokAda { get; set; }
    public ScanDetail TrendMicro { get; set; }
    public ScanDetail SUPERAntiSpyware { get; set; }
    public ScanDetail nProtect { get; set; }
    public ScanDetail NANOAV { get; set; }
 }


 foreach (var prop in report.scan_results.scan_details.GetType().GetProperties())
 {
    if(prop.PropertyType is ScanDetail)
    {
        var scan_detail = prop.GetValue(report.scan_results.scan_details) as ScanDetail;
        Console.WriteLine("{0}.{1} = {2}", prop.Name, "threat_found", scan_detail.threat_found);    
    }
    
 }

【讨论】:

  • 谢谢,但是scan_detail 没有threat-found 属性。
猜你喜欢
  • 2021-06-02
  • 1970-01-01
  • 2016-12-19
  • 2022-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-04
相关资源
最近更新 更多