没有setter的属性无法分配

时间:2013-12-24 17:08:52

标签: c# specflow

我正在编写一个包含三列的规格流测试... 例如:

|Field  | Key       | Value |
|A      | aa        |   ""  |
|       | bb        |   ""  |
|       | cc        |   33  |
|       | dd        |   1   |
|B      | aa        |   5   |
|       | bb        |   4   |
|       | cc        |   2   |
|       | dd        |   ""  |

我添加了此方法来从表中读取数据:

      var elements = new Dictionary<string, Dictionary<string,string>>().ToList();
        var data = table.CreateSet<SpecFlowData>();
        elements.AddRange(collection: from item in data
                              let field =item.Field
                              let key = item.Key
                              let value = item.Value
                               select new Dictionary<string, Dictionary<string,string>>()
                                  {
                                      Keys = field,
                                      Values = new Dictionary<string,string>()
                                          {
                                              Keys= key,
                                              Values = value

                                          } 

                                  }

                              );

我正在尝试为第一列中的每个字段分配一个键值对。但是在构建代码时,我收到错误“没有setter的属性”。任何想法都表示赞赏。谢谢。

2 个答案:

答案 0 :(得分:3)

您无法直接将KeysValues分配给Dictionary<>。但您可以使用LINQ将data投影到Dictionary的{​​{1}} {/ p>}

Dictionaries

答案 1 :(得分:2)

您无法专门设置Dictionary<,>.KeysDictionary<,>.Values属性。它们只是get属性。您需要遍历您的值并使用Add(Key, Value)或使用LINQ ToDictionary<TSource, TKey>方法。

相关问题