如何创建一个值为两种不同数据类型的keyvaluepair ?

时间:2021-01-06 17:02:26

I wrote a method that returns

我写了一个返回的方法

List<KeyValuePair<CommandType, List<string>>>

CommandType is of Type enum

命令类型是enum类型。

public enum CommandType
{
    Programmed,
    Manual
}

My issue is that the value in the KeyValuePair sometimes is an enum, and sometimes it is a list of strings, but I need to keep all the KeyValuePair in one list.

我的问题是KeyValuePair中的值有时是enum,有时是字符串列表,但我需要将所有KeyValuePair保存在一个列表中。

currently, I pass the value as an object in the keyvaluepair and when the method returns the list and an iterate through it, based on the key, I cast the value back to its original type.

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

Is there a better way to implement this?

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

here is a sample code

这是一个示例代码

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;
}

1 个解决方案

#1


2  

If you want to use a single storage for different types, since the type of values can be determined only at run-time, then you should use object type for boxing values and then when you need to work with a value in a typed manner, check for its type and unbox it to desired type and use it.

如果你想使用一个存储为不同类型,因为只有在运行时才能确定类型的值,那么您应该使用拳击值的对象类型,然后当你需要使用一个值类型的方式,检查其类型,从箱子中取出所需的类型和使用它。

So you can use one of these data structures based on your requirements:

因此,您可以根据您的需求使用其中一个数据结构:

  • Dictionary<CommandType, object> ← The key should be unique.
  • 字典< CommandType、对象>←键应该是唯一的。
  • List<KeyValuePair<CommandType, object>> ← There key of pair doesn't need to be unique.
  • 列表< KeyValuePair < CommandType、对象> >←键的对不需要是唯一的。

Note: You probably can imagine solutions like creating a common base class BaseType and derive two different ListContainer and EnumContainer from the BaseType and creating ListContainer and EnumContainer at runtime and store in a Dictionary<CommandType, BaseType>. Such structures probably just can help you to limit the storage to desired type rather than using object.

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

#1


2  

If you want to use a single storage for different types, since the type of values can be determined only at run-time, then you should use object type for boxing values and then when you need to work with a value in a typed manner, check for its type and unbox it to desired type and use it.

如果你想使用一个存储为不同类型,因为只有在运行时才能确定类型的值,那么您应该使用拳击值的对象类型,然后当你需要使用一个值类型的方式,检查其类型,从箱子中取出所需的类型和使用它。

So you can use one of these data structures based on your requirements:

因此,您可以根据您的需求使用其中一个数据结构:

  • Dictionary<CommandType, object> ← The key should be unique.
  • 字典< CommandType、对象>←键应该是唯一的。
  • List<KeyValuePair<CommandType, object>> ← There key of pair doesn't need to be unique.
  • 列表< KeyValuePair < CommandType、对象> >←键的对不需要是唯一的。

Note: You probably can imagine solutions like creating a common base class BaseType and derive two different ListContainer and EnumContainer from the BaseType and creating ListContainer and EnumContainer at runtime and store in a Dictionary<CommandType, BaseType>. Such structures probably just can help you to limit the storage to desired type rather than using object.

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