如何在 foreach 中获取列表的字段名称?

How to get the field names of a list inside foreach?

我有以下 class:

public class ListOfVariablesToSave : List<List<int>>
{
    public List<int> controlDB { get; set; }
    public List<int> interacDB { get; set; }

    public ListOfVariablesToSave()
    {
        controlDB = new List<int> { 1, 2, 3 };
        interacDB = new List<int> { 21, 22, 23 };

        Add(controlDB);
        Add(interacDB);
    }
}

以及将其内容写入文本文件的以下代码:

ListOfVariablesToSave myListOfVariablesToSave = new ListOfVariablesToSave();

StreamWriter myFile = new StreamWriter("fileName.txt");
foreach (List<int> DB in myListOfVariablesToSave)
{
    foreach (int varToSave in DB)
    {
        myFile.WriteLine(varToSave);
    }
}
myFile.Close();

我得到的是:

1
2
3
21
22
23

我想得到的是:

controlDB
1
2
3
interacDB
21
22
23

是否可以通过在第一个 foreach 之后添加一行代码来做到这一点?

我会考虑做这样的事情:

public class ListOfVariablesToSave : List<ListOfThings<int>>
{
    public ListOfThings<int> controlDB { get; set; }
    public ListOfThings<int> interacDB { get; set; }

    public ListOfVariablesToSave()
    {
        controlDB = new ListOfThings<int>() { 1, 2, 3  };
        controlDB.Name = "controlDB";
        interacDB = new ListOfThings<int>() { 21, 22, 23 };
        interacDB.Name = "interacDB";

        Add(controlDB);
        Add(interacDB);
    }

}

public class ListOfThings<T> : List<T>
{
    public string Name { get; set; }

    public ListOfThings() : base() { }
}

您的 ListOfVariablesToSave 不是派生自 List<List<int>>,而是创建另一个派生自 List<int> 的 class,并添加您的名字 属性。

然后您可以像这样迭代:

var lists = new ListOfVariablesToSave();
foreach (var list in lists) 
{
    Console.WriteLine(list.Name);
    foreach (var i in list)
    {
        Console.WriteLine(i);
    }
}

这是使用反射的技巧(只需更改 Console.WriteLine 以转到您的文件):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

public class Program
{
    public static void Main()
    {
        ListOfVariablesToSave myListOfVariablesToSave = new ListOfVariablesToSave();

        foreach (PropertyInfo pi in myListOfVariablesToSave
                 .GetType()
                 .GetProperties( BindingFlags.Public | 
                                 BindingFlags.Instance | 
                                 BindingFlags.DeclaredOnly )
                 // Only get the properties of type List<int>
                 // Actual PropertyType looks like: 
                 //   System.Collections.Generic.List`1[System.Int32]
                 .Where(p => p.PropertyType.ToString().Contains("List") && 
                        p.PropertyType.ToString().Contains("System.Int32"))
                )
        {
            Console.WriteLine(pi.Name);
            (pi.GetValue(myListOfVariablesToSave) as List<int>).ForEach(i => Console.WriteLine(i));
        }
    }
}

public class ListOfVariablesToSave : List<List<int>>
{
    public List<int> controlDB { get; set; }
    public List<int> interacDB { get; set; }
    // Added this property to show that it doesn't get used in the above foreach
    public int Test {get; set;}

    public ListOfVariablesToSave()
    {
        controlDB = new List<int> { 1, 2, 3 };
        interacDB = new List<int> { 21, 22, 23 };
        Add(controlDB);
        Add(interacDB);
    }
}

结果:

controlDB
1
2
3
interacDB
21
22
23

在此处查看工作示例...https://dotnetfiddle.net/4yyc8Q

更新

使用 Matt Burland 的 class 结构不那么麻烦(同样,只需替换 Console.WriteLine 即可转到您的文件)

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        ListOfVariablesToSave myListOfVariablesToSave = new ListOfVariablesToSave();

        myListOfVariablesToSave.ForEach(m => {
            Console.WriteLine(m.Name);
            m.ForEach(m1 => Console.WriteLine(m1));
        });
    }
}

public class ListOfVariablesToSave : List<ListOfThings<int>>
{
    public ListOfThings<int> controlDB { get; set; }
    public ListOfThings<int> interacDB { get; set; }

    public ListOfVariablesToSave()
    {
        controlDB = new ListOfThings<int>() { 1, 2, 3  };
        controlDB.Name = "controlDB";
        interacDB = new ListOfThings<int>() { 21, 22, 23 };
        interacDB.Name = "interacDB";

        Add(controlDB);
        Add(interacDB);
    }

}

public class ListOfThings<T> : List<T>
{
    public string Name { get; set; }

    public ListOfThings() : base() { }
}

结果:

controlDB
1
2
3
interacDB
21
22
23

在此处查看工作示例...https://dotnetfiddle.net/CKsIFo

以下代码不是最好的方法,但希望对您有更多帮助:

型号:

class ListOfVariablesToSave 
{
    public List<int> controlDB { get; set; }
    public List<int> interacDB { get; set; }
}

代码:

static void Main()
    {
        ListOfVariablesToSave model = new ListOfVariablesToSave();
        List<int> controlDB = new List<int> { 1, 2, 3 };
        List<int> interacDB = new List<int> { 21, 22, 23 };
        model.controlDB = controlDB;
        model.interacDB = interacDB;


        StreamWriter myFile = new StreamWriter("fileName.txt");
        foreach (var prop in model.GetType().GetProperties())
        {
            myFile.WriteLine(prop.Name);
            foreach (var prop1 in model.GetType().GetProperties())
            {
                if (String.Compare(prop1.Name, prop.Name) == 0)
                {
                    foreach (int varToSave in (List<int>)prop1.GetValue(model, null))
                    {
                        myFile.WriteLine(varToSave);
                    }
                }
            }

        }
        myFile.Close();
    }