Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.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# 如何将SpecFlow表转换为Dictionaryc_C#_Dictionary_Specflow - Fatal编程技术网

C# 如何将SpecFlow表转换为Dictionaryc

C# 如何将SpecFlow表转换为Dictionaryc,c#,dictionary,specflow,C#,Dictionary,Specflow,我必须执行以下SpecFlow代码: And I get and validate parameters | ParameterName| Value | Answers | Mandatory | Meta | Modified | ReadOnly | Submit | SubmitValues | Tag | | SurName | | | true | | false | fal

我必须执行以下SpecFlow代码:

    And I get and validate parameters
        | ParameterName| Value | Answers | Mandatory | Meta | Modified | ReadOnly | Submit | SubmitValues | Tag    |
        | SurName      |       |         | true      |      | false    | false    | true   |              | input  |
        | Name         |       |         | true      |      | false    | false    | false  |              | input  |
.....
我希望这个表将它转换成一个字典,头列将是键,其余的信息将是值。我硬编码了一些值:

Dictionary<string, List<string>> dictionary = new Dictionary<string, List<string>>();
dictionary.Add("ParameterName", new List<string> {"SurName", "Name", "Age"});
dictionary.Add("Value", new List<string> { "", "", "" });
dictionary.Add("Answers", new List<string> { "", "", "" });
dictionary.Add("Mandatory", new List<string> { "true", "true", "true" });
dictionary.Add("Meta", new List<string> { "", "", "" });
dictionary.Add("Modified", new List<string> { "false", "false", "false" });
dictionary.Add("ReadOnly", new List<string> { "false", "false", "false" });
dictionary.Add("Submit", new List<string> { "true", "false", "true" });
dictionary.Add("SubmitValues", new List<string> { "", "", "" });
dictionary.Add("Tag", new List<string> { "input", "input", "select" });
但是实际的表有很多值,我需要对所有值都这样做,它们可能会改变,这就是为什么我不需要硬编码字典的原因。
我怎样才能做到这一点呢?

在这样的词典中,检索同一行相当麻烦,您应该为值列表编制索引

相反,我将创建一个列表,其中TestParameters类包含一个具有普通强类型属性的行:

public class TestParameters
{
    public string ParameterName { set; set; }
    public int Value { set; set; }
    public bool Mandatory { set; set; }
    // etc.
}
现在你有了这样一个测试步骤:

[Given(@"I get and validate parameters")]
public void GetParameters(Table parameters)
{
}
只需将表格替换为更具体的类型:

[Given(@"I get and validate parameters")]
public void GetParameters(List<TestParameters> parameters)
{
}
只需在助手类中定义一个表->列表转换步骤:

[Binding]
public class Transformations
{
    [StepArgumentTransformation]
    public List<TestParameters> GetTestParameters(Table table)
    {
        return table.Rows.Select(row => new TestParameters
        {
            // string prop
            ParameterName = row["ParameterName"],

            // int prop
            Value = !String.IsNullOrEmpty(row["Value"]) ? Int32.Parse(row["Value"]) : 0,

            // bool prop
            Mandatory = row["Mandatory"]?.ToLowerInvariant() == "true"

            // TODO: other properties
        }).ToList();
    }
}

当然,转换的结果也可以是Dictionary,如果您确实坚持…

您还可以使用TechTalk.SpecFlow.Assist命名空间中的CreateSet扩展方法。 请查看此处的文档:

小例子:

CreateSet是表外的一个扩展方法,它将转换 将表数据转换为一组对象。例如,如果您有以下内容 步骤:

可以将表中的数据转换为一组对象,如下所示:


现在回答这个问题已经太迟了,但可能对其他人有帮助

在收集中处理此类数据的更好方法应该是使用format List>

下面的扩展方法应该在这方面有所帮助-

    public static List<Dictionary<string, string>> ConvertToDictionaryList(this Table dt)
    {
        var lstDict = new List<Dictionary<string, string>>();

        if (dt!=null)
        {
            var headers = dt.Header;

            foreach (var row in dt.Rows)
            {
                var dict = new Dictionary<string, string>();
                foreach (var header in headers)
                {
                    dict.Add(header, row[header]);
                }

                lstDict.Add(dict);
            }
        }

        return lstDict;
    }
但如果您仍然想使用Dictionary>,那么您可以使用以下代码段

    public static List<Dictionary<string, string>> ConvertToDictionaryList(this Table dt)
    {
        var lstDict = new Dictionary<string, List<string>>();

        if (dt != null)
        {
            var headers = dt.Header;

            foreach (var row in dt.Rows)
            {
                foreach (var header in headers)
                {
                    if (lstDict.ContainsKey(header))
                    {
                        lstDict[header].Add(row[header]);
                    }
                    else
                    {
                        lstDict.Add(header, new List<string> { row[header] });
                    }
                }
            }
        }

        return lstDict;

    }

在Transformations类中,我收到一个错误:严重性代码描述项目文件行错误CS1061'TableRow'不包含'GetColumn'的定义,并且找不到接受'TableRow'类型的第一个参数的扩展方法'GetColumn'。是否缺少using指令或程序集引用?抱歉,GetColumn是我实现的一个扩展方法,用于处理不存在的列。我编辑了答案。
    public static List<Dictionary<string, string>> ConvertToDictionaryList(this Table dt)
    {
        var lstDict = new List<Dictionary<string, string>>();

        if (dt!=null)
        {
            var headers = dt.Header;

            foreach (var row in dt.Rows)
            {
                var dict = new Dictionary<string, string>();
                foreach (var header in headers)
                {
                    dict.Add(header, row[header]);
                }

                lstDict.Add(dict);
            }
        }

        return lstDict;
    }
    public static List<Dictionary<string, string>> ConvertToDictionaryList(this Table dt)
    {
        var lstDict = new Dictionary<string, List<string>>();

        if (dt != null)
        {
            var headers = dt.Header;

            foreach (var row in dt.Rows)
            {
                foreach (var header in headers)
                {
                    if (lstDict.ContainsKey(header))
                    {
                        lstDict[header].Add(row[header]);
                    }
                    else
                    {
                        lstDict.Add(header, new List<string> { row[header] });
                    }
                }
            }
        }

        return lstDict;

    }