[英]How to get the field names of a list inside foreach?
我有以下課程:
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() { }
}
而不是從List<List<int>>
派生您的ListOfVariablesToSave
,而是創建另一個從List<int>
派生並添加您的name屬性的類。
然后,您可以像這樣迭代:
var lists = new ListOfVariablesToSave();
foreach (var list in lists)
{
Console.WriteLine(list.Name);
foreach (var i in list)
{
Console.WriteLine(i);
}
}
下面的代碼不是最好的方法,但希望它能對您有所幫助:
模型:
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();
}
這使用反射實現了技巧(只需將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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.