0

以下列表是在名为“实体”的类中创建的,它们是私有的。我想退回它们(并在另一个班级中使用)我可以通过以下方式做些什么:

    public List<string> getCMList()
    {
        return exceptionsCM;
    }

但是如果有 10 个列表,我必须为所有这些列表编写一个公共的“get”方法。是否可以编写一个将字符串作为输入并返回该列表的方法?应该类似于..

public List<string> getList(.....)
{
     return exceptionCM;
} //if we call getList(exceptionCM).


        exceptionsCM = new List<string>();
        exceptionsCM.Add("CM12");
        exceptionsCM.Add("CM701");
        exceptionsCM.Add("CM901/CM30");
        exceptionsCM.Add("CM901K");
        exceptionsCM.Add("CM1101");
        exceptionsCM.Add("CM1101K");

        //List with Multi Mill exceptions, they are included in the new MultiB
        exceptionsMultiB = new List<string>();
        exceptionsMultiB.Add("Multi650/450");
        exceptionsMultiB.Add("Multi660/630");
        exceptionsMultiB.Add("Multi650/800");
        exceptionsMultiB.Add("Multi650/1000");

        //List with Multi Mill exceptions, they are included in the new Multi01
        exceptionsMulti01 = new List<string>();
        exceptionsMulti01.Add("Multi301");
        exceptionsMulti01.Add("Multi601");
        exceptionsMulti01.Add("Multi801");
        exceptionsMulti01.Add("Multi1001");
4

4 回答 4

2

你可以把它们都放在一个静态 Dictionary> 中

Dictionary<string, List<string>> ListstDict = new Dictionary<string, List<string>>();
ListsDict.Add("exceptionsCM", exceptionsCM);
...

然后你可以写一个这样的方法:

public List<string> GetList(string name)
{
    return ListsDict.ContainsKey(name) ? ListsDict[name] : null;
}

但是话又说回来,如果您可以简单地获取这些列表,那么私有列表的意义何在?使用私有 setter 将它们声明为公共属性会更容易

public List<string> exceptionsCM { get; private set; }
于 2012-09-18T10:00:22.640 回答
2

你可以Dictionary<K,V>为此使用一个类

private Dictionary<string, List<<string>> dictionary = 
    new Dictionary<string, List<<string>>();

//fill that dictionary
...

//get list from a dictionary by list id
public List getList(string listId) 
{
    return dictionary[listId];
}
于 2012-09-18T09:57:06.997 回答
2

您可以将所有列表放入字典中:

// your code here
dictionary = new Dictionary<string, List<string>>();
dictionary.Add("exceptionsCM", exceptionsCM);

那么getList方法就这么简单:

public List<string> getList(string name)
{
    return dictionary[name];
}
于 2012-09-18T09:57:37.277 回答
0

使用字典和枚举。

枚举用于列表键而不是字符串可以使您的代码对键重命名更加健壮。即,当您在Entities类中将MultiB重命名为MultiB1时,编译器不会显示有关调用 GetList("MultiB") 的任何错误和警告。如果使用枚举编译器会显示这些错误。

internal class Entities
{
    private Dictionary<ListKey, List<string>> _liststDict =
      new Dictionary<ListKey, List<string>>
        {
          {ListKey.Cm, new List<string> {"CM12", "CM701", "CM901/CM30", "CM901K", "CM1101", "CM1101K"}},
          {ListKey.MultiB, new List<string> {"Multi650/450", "Multi660/630", "Multi650/800", "Multi650/1000"}},
          {ListKey.Multi01, new List<string> {"Multi301", "Multi601", "Multi801", "Multi1001"}}
        };

    public List<string> GetList(ListKey key)
    {
      return _liststDict[key];
    }

    internal enum ListKey
    {
      Cm,
      MultiB,
      Multi01
    }
}


internal class EntitiesTester
{
    public static void Do()
    {
      Entities entities = new Entities();

      Console.Out.WriteLine("CM count = {0}", entities.GetList(Entities.ListKey.Cm).Count);
      Console.Out.WriteLine("MultiB count = {0}", entities.GetList(Entities.ListKey.MultiB).Count);
      Console.Out.WriteLine("Multi01 count = {0}", entities.GetList(Entities.ListKey.Multi01).Count);
    }
}
于 2012-09-18T10:51:35.490 回答