0

我有以下课程:

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

4

3 回答 3

2

我会考虑做这样的事情:

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>并添加您的 name 属性的类。

然后你可以像这样迭代:

var lists = new ListOfVariablesToSave();
foreach (var list in lists) 
{
    Console.WriteLine(list.Name);
    foreach (var i in list)
    {
        Console.WriteLine(i);
    }
}
于 2015-06-03T02:21:32.963 回答
2

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

模型:

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();
    }
于 2015-06-03T03:25:29.860 回答
1

这使用反射完成了技巧(只需更改 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 的类结构不那么麻烦(同样,只需替换 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

于 2015-06-03T02:41:17.897 回答