【问题标题】:C# how to implement IEnumerable for nested classesC#如何为嵌套类实现IEnumerable
【发布时间】:2019-12-18 12:31:29
【问题描述】:

我有一组相当简单的类,只有属性,例如:

using System;               //main data types
using System.Reflection;    //to iterate through all properties of an object
using System.Collections;   //for IEnumerable implementation?

namespace ConsoleApp1
{
    public class WholeBase //: IEnumerable ?
    {
        public SomeHeaders Headers { get; set; }
        public SomeBody Body { get; set; }
    }

    public partial class SomeHeaders
    {
        public string HeaderOne { get; set; }
        public string HeaderTwo { get; set; }
    }

    public partial class InSet
    {
        public string AllForward { get; set; }
        public string Available { get; set; }
    }

    public partial class SomeBody
    {
        public InSet MySet { get; internal set; }
        public Boolean CombinedServiceIndicator { get; set; }
        public int FrequencyPerDay { get; set; }
        public string ValidUntil { get; set; }
    }

我试图获取所有属性和值,但似乎我被卡住了,因为 IEnumerable 或缺少某些东西。到目前为止,这是我尝试过的:填充属性并尝试遍历所有属性和值,但是不起作用...

  public class Program
{
    //...
    public static void Main(string[] args)
    {
        WholeBase NewThing = new WholeBase();
        NewThing.Headers = new SomeHeaders { HeaderOne = "First", HeaderTwo = "Second" };

        NewThing.Body = new SomeBody
        {
            MySet = new InSet { AllForward = "YES", Available = "YES"},
            CombinedServiceIndicator = false,
            FrequencyPerDay = 10,
            ValidUntil = "2019-12-31"
        };

        void SeeThrough(WholeBase myBase)
        {
            //iterate through all the properties of NewThing
            foreach (var element in myBase)
            {
                foreach (PropertyInfo prop in myBase.GetType().GetProperties())
                {
                    var type = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;
                    Console.WriteLine(prop.GetValue(element, null).ToString());
                }
            }
        };
    }
}

【问题讨论】:

  • 您的类不需要实现IEnumerable 即可获取其所有属性。你目前的方法有什么问题?你有什么错误吗?
  • 你不需要两个循环。一个单一的就足够了(顺便说一下,内部的)
  • 要使用 IEnumerable,您需要多个项目。看起来您的所有属性都是单例。如果可能的话,最简单的方法就是将项目设置为数组或列表。
  • @jdweng 非常感谢您的建议,我是新手,不知道如何使用嵌套对象实现列表,特别是 public class WholeBase 示例..
  • 你需要一个变量: List WholeBase = new List();然后将项目添加到列表中。

标签: c#


【解决方案1】:

好吧,您似乎在想“嗯,我将遍历 'A' 类的所有属性值,同时使用反射来获取 'A' 类的所有属性,然后对于每个属性,我将显示它价值。”

这里有很多问题。

首先 - 循环所有值只能使用实现接口IEnumerable 的对象,但您并不真正需要它。由于您使用反射获取其所有属性,因此您也可以使用它来获取值:

foreach (PropertyInfo prop in myBase.GetType().GetProperties())
{
    // this returns object
    var element = prop.GetValue(myBase, null);
    Console.WriteLine(element);
}

其次 - ToString() 不知道如何显示对象的字段,除非对象覆盖它。

虽然上面的代码可以编译和工作,但是因为元素是一个对象而不是原始类型,除非你重写了方法.ToString,这个调用Console.WriteLine只会显示类型的名称。

您可以再次循环遍历该对象的所有属性,并最终为每个属性获取一个值:

foreach (var childProperty in element.GetType().GetProperties())
{
   Console.WriteLine(childProperty.GetValue(element, null).ToString());
}

【讨论】:

  • @HimBromBeere 是的,我很困惑。谢谢
  • @Fabjan 是的,你猜到了我的意图,非常感谢你提供的代码,它很有帮助,我快到了。我想将两者结合起来,有点像:Console.WriteLine(element + "=" + childProperty.GetValue(element).ToString()); 但显然这样element, null 必须改变,一个想法?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多