我有以下课程:
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
之后添加一行代码来实现此目的?
答案 0 :(得分: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>
的类,并添加您的名称属性。
然后你可以像这样迭代:
var lists = new ListOfVariablesToSave();
foreach (var list in lists)
{
Console.WriteLine(list.Name);
foreach (var i in list)
{
Console.WriteLine(i);
}
}
答案 1 :(得分: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();
}
答案 2 :(得分: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