C# 遍历类为元素的列表
using System; using System.Windows.Forms; using ClassLibrary2; using System.Collections.Generic; namespace messageshow { public partial class Form1 : Form { public Form1() { InitializeComponent(); Adda(); } private void listView1_SelectedIndexChanged(object sender, EventArgs e) { } public void Adda() { Class1 class1 = new Class1 { Text = "a", Project = "b", Object = "c", Location = "d", wRONGTYPE = Value.WRONGTYPE.WARNING }; Class1 class2 = new Class1 { Text = "文本", Project = "项目", Object = "对象", Location = "位置", wRONGTYPE = Value.WRONGTYPE.WARNING }; List<Class1> class1s = new List<Class1> { class1, class2 }; Value.Collection.Add(Value.WRONGTYPE.WARNING, class1s); // li.Text = "1"; foreach (Class1 u in Value.Collection[Value.WRONGTYPE.WARNING]) { string[] sub = new string[] { "", u.Text, u.Project, u.Object, u.Location }; ListViewItem li; li = new ListViewItem(sub); // this.listView3.Items.Add(li); } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ClassLibrary2 { public static class Value { public enum WRONGTYPE { WRONG, WARNING } public static Dictionary<WRONGTYPE, List<Class1>> Collection = new Dictionary<WRONGTYPE, List<Class1>>(); } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ClassLibrary2 { public class Class1 { public string Text; public string Project; public string Object; public string Location; public Value.WRONGTYPE wRONGTYPE; } }