C# 如何为嵌套类实现 IEnumerable

C# how to implement IEnumerable for nested classes

提问人:Nick 提问时间:12/18/2019 更新时间:12/18/2019 访问量:572

问:

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

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());
                }
            }
        };
    }
}
c#

评论

2赞 MakePeaceGreatAgain 12/18/2019
您的类不需要实现即可获取其所有属性。您目前的方法是什么?你收到任何错误吗?IEnumerable
0赞 MakePeaceGreatAgain 12/18/2019
您不需要两个循环。一个 simngle 就足够了(顺便说一句,内部的)
0赞 jdweng 12/18/2019
若要使用 IEnumerable,您需要多个项目。看起来你所有的属性都是单例的。如果可能的话,最简单的方法是将项目设为数组或列表。
0赞 Nick 12/18/2019
@jdweng 非常感谢您的建议,我是新手,不知道如何使用嵌套对象实现列表,尤其是例如。public class WholeBase
0赞 jdweng 12/18/2019
你需要一个变量: List<WholeBase> wholeBase = new List<WholeBase>();,然后将项目添加到列表中。

答:

1赞 Fabjan 12/18/2019 #1

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

这里有很多问题。

首先 - 只有使用 Implements 接口的对象才能遍历所有值,但您实际上并不需要它。由于您使用反射获取其所有属性,因此您也可以使用它来获取值:IEnumerable

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

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

尽管上面的代码可以编译并工作,但由于 element 是对象而不是基元类型,除非您重写了该方法,否则此调用将仅显示该类型的名称。.ToStringConsole.WriteLine

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

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

评论

0赞 Fabjan 12/18/2019
@HimBromBeere 是的,我搞糊涂了。谢谢
0赞 Nick 12/18/2019
@Fabjan 是的,你猜到了我的意图,非常感谢你的代码,它太有帮助了,我快到了。我想把两者结合起来,有点:但显然必须以这种方式改变,一个想法?Console.WriteLine(element + "=" + childProperty.GetValue(element).ToString());element, null