-2
4

2 回答 2

5

你想要一个Dictionary<char, ObservableCollection<SampleObject>>,每个都有不同的集合char

于 2013-05-14T13:50:24.077 回答
0

如果您的列表是在类级别声明的,则可以使用反射来获取对它们的引用(我仍然认为字典是更好的选择)。

简单的例子:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private List<string> lista = new List<string>();

    private void button1_Click(object sender, EventArgs e)
    {
        Console.WriteLine("Before:");
        foreach (string value in lista)
        {
            Console.WriteLine(value);
        }

        for (char i = 'a'; i < 'z'; i++)  
        {  
            string name = "list" + i;
            System.Reflection.FieldInfo fi = this.GetType().GetField(name, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public);
            if (fi != null && fi.FieldType.Equals(typeof(List<string>)))
            {
                List<string> referenceToList = (List<string>)fi.GetValue(this);
                referenceToList.Add("Hello!");
            }
        }

        Console.WriteLine("After:");
        foreach (string value in lista)
        {
            Console.WriteLine(value);
        }
    }
}
于 2013-05-14T15:38:30.150 回答