Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用两种不同数据类型的值创建keyvaluepair?_C#_.net_Winforms - Fatal编程技术网

C# 如何使用两种不同数据类型的值创建keyvaluepair?

C# 如何使用两种不同数据类型的值创建keyvaluepair?,c#,.net,winforms,C#,.net,Winforms,我编写了一个返回 List<KeyValuePair<CommandType, List<string>>> 我的问题是KeyValuePair中的值有时是enum,有时是字符串列表,但我需要将所有KeyValuePair保留在一个列表中 目前,我将该值作为对象传递到keyvaluepair中,当该方法返回列表并根据键对其进行迭代时,我将该值转换回其原始类型 有没有更好的方法来实现这一点 下面是一个示例代码 public enum ProgrammedCo

我编写了一个返回

List<KeyValuePair<CommandType, List<string>>>
我的问题是
KeyValuePair
中的值有时是
enum
,有时是字符串列表,但我需要将所有
KeyValuePair
保留在一个列表中

目前,我将该值作为对象传递到keyvaluepair中,当该方法返回列表并根据键对其进行迭代时,我将该值转换回其原始类型

有没有更好的方法来实现这一点

下面是一个示例代码

public enum  ProgrammedCommands
{
    Sntp,
    Snmp,
}
private List<KeyValuePair<CommandType, object>> GetCommandsFromTemplate(string[] templateLines)
{
    var list = new List<KeyValuePair<CommandType, object>>();

    if (templateLines != null)
        for (int lineIndex = 0; lineIndex < templateLines.Length; lineIndex++)
        {
            if (templateLines[lineIndex].Contains("!*") && templateLines[lineIndex].Contains("*!"))
            {
                KeyValuePair<CommandType, object> ProgrammedSetting;
                List<string> programmedCommandList;
                if (templateLines[lineIndex].Contains("SNTP - SNTP Server Commands"))
                {

                    ProgrammedSetting = new KeyValuePair<CommandType, object>(CommandType.Programmed, ProgrammedCommands.Sntp);
                    list.Add(ProgrammedSetting);
                }

                else if (templateLines[lineIndex].Contains("MANUAL"))
                {
                    lineIndex++;
                    List<string> manual = new List<string>();
                    while (true)
                    {
                        if (lineIndex >= templateLines.Length)
                            break;
                        if (templateLines[lineIndex].Contains("!!["))
                            lineIndex++;
                        else if (templateLines[lineIndex].Contains("]!!"))
                            break;
                        else
                        {
                            manual.Add(templateLines[lineIndex]);
                            lineIndex++;
                        }
                    }
                    ProgrammedSetting = new KeyValuePair<CommandType, object>(CommandType.Manual, manual);
                    list.Add(ProgrammedSetting);
                }
            }
        }
    return list;
}
public enum ProgrammedCommands
{
Sntp,
Snmp,
}
私有列表GetCommandsFromTemplate(字符串[]templateLines)
{
var list=新列表();
if(模板行!=null)
对于(int-lineIndex=0;lineIndex=templateLines.Length)
打破
if(templateLines[lineIndex]。包含(!![“”)
lineIndex++;
else if(templateLines[lineIndex]。包含(“]!!”))
打破
其他的
{
手动添加(模板行[lineIndex]);
lineIndex++;
}
}
ProgrammedSetting=新的KeyValuePair(CommandType.Manual,Manual);
list.Add(ProgrammedSetting);
}
}
}
退货清单;
}

如果要对不同类型使用单个存储,因为只能在运行时确定值的类型,那么应该使用
对象
类型来装箱值,然后当需要以键入方式处理值时,检查其类型并将其取消装箱到所需类型并使用它

因此,您可以根据需要使用以下数据结构之一:

  • 字典
    ← 密钥应该是唯一的
  • 列表
    ← 这对密钥不需要是唯一的

注意:您可能可以想象这样的解决方案:在运行时创建一个公共基类
BaseType
,从
BaseType
派生两个不同的
ListContainer
EnumContainer
,并在
字典
。这样的结构可能只会帮助您将存储限制为所需的类型,而不是使用对象。

您是否正在查找
字典
列表
?编写一个具有所需属性的类,并返回该类的
列表
。你所拥有的东西很难阅读,也很难维护。这是你的生命,但只要你在编写这段代码,你就可以节省一点时间,而这是以无休止的麻烦为代价的
KeyValuePair
在这里是一个非常不合适的选择。@EdPlunkett我是来学习的,而“疯狂”这个词并不能教会我任何东西。但是像这样的话:没有效率,糟糕的做法。。。跟着一些简单的解释可以教很多东西。@EdPlunkett列表不能解决问题,因为当我初始化要添加项目的列表时,我需要将t设置为
ProgrammedCommand
列表
,因此不能将两种类型添加到同一个列表中。我想我需要创建一个类,它有两个属性,一个是
ProgrammedCommand
,另一个是
object
类型的属性,然后我可以维护从该类实例化的对象列表,这将使代码更干净。我所需要做的就是按顺序遍历列表,其他什么都不做!因此,感谢EdPunkett的建议和Reza的帮助Aghaei@kat13301)用户没有尝试重选
enum
string
。他试图存储
enum
List
,因此使用
string
没有意义。2) 当您可以使用对象来装箱/取消装箱值时,将枚举存储为字符串并将其从字符串转换回字符串似乎并不高效。3) 有时,基于某人的观点,解决方案是丑陋或笨拙的,因为它们似乎没有任何附加值。4) 毕竟,在我看来,你似乎更生气,而不是对真正的原因感到好奇。这就是为什么我给你写了一条评论,如果你不喜欢就忽略它:)
public enum  ProgrammedCommands
{
    Sntp,
    Snmp,
}
private List<KeyValuePair<CommandType, object>> GetCommandsFromTemplate(string[] templateLines)
{
    var list = new List<KeyValuePair<CommandType, object>>();

    if (templateLines != null)
        for (int lineIndex = 0; lineIndex < templateLines.Length; lineIndex++)
        {
            if (templateLines[lineIndex].Contains("!*") && templateLines[lineIndex].Contains("*!"))
            {
                KeyValuePair<CommandType, object> ProgrammedSetting;
                List<string> programmedCommandList;
                if (templateLines[lineIndex].Contains("SNTP - SNTP Server Commands"))
                {

                    ProgrammedSetting = new KeyValuePair<CommandType, object>(CommandType.Programmed, ProgrammedCommands.Sntp);
                    list.Add(ProgrammedSetting);
                }

                else if (templateLines[lineIndex].Contains("MANUAL"))
                {
                    lineIndex++;
                    List<string> manual = new List<string>();
                    while (true)
                    {
                        if (lineIndex >= templateLines.Length)
                            break;
                        if (templateLines[lineIndex].Contains("!!["))
                            lineIndex++;
                        else if (templateLines[lineIndex].Contains("]!!"))
                            break;
                        else
                        {
                            manual.Add(templateLines[lineIndex]);
                            lineIndex++;
                        }
                    }
                    ProgrammedSetting = new KeyValuePair<CommandType, object>(CommandType.Manual, manual);
                    list.Add(ProgrammedSetting);
                }
            }
        }
    return list;
}