【问题标题】:"does not contain a definition...and no extension method.." error“不包含定义……也没有扩展方法……”错误
【发布时间】:2011-08-24 15:31:33
【问题描述】:

我收到以下错误消息:

'System.Collections.Generic.Dictionary<int,SpoofClass>' does not
contain a definition for 'biff' and no extension method 'biff'
accepting a first argument of type
'System.Collections.Generic.Dictionary<int,SpoofClass>' could be found
(are you missing a using directive or an assembly reference?)

我对此进行了检查,发现this 问题似乎与我有类似(如果不相同)的问题。但是,我尝试了接受的答案中提供的解决方案,但仍然没有提出任何建议。这就像我缺少一个 using 语句,但我几乎可以肯定我拥有我需要的所有 using。

以下是一些产生错误的代码:

using locationOfSpoofClass;
...

Dictionary<int, SpoofClass> cart = new Dictionary<int, SpoofClass>();
foreach (var item in dbContext.DBView)
{
    cart.biff = item.biff;
    ...
}

SpoofClass 文件:

namespace locationOfSpoofClass
{
    public class SpoofClass
    {
        public int biff { get; set; }
        ...
    }
}

对不起,如果我对变量的重命名和诸如此类的东西令人困惑。如果它不可读,或者太难理解,或者如果其他信息与解决方案相关,请告诉我。谢谢!

【问题讨论】:

    标签: c# .net wpf compiler-errors


    【解决方案1】:

    问题出在这部分:cart.biffcartDictionary&lt;int, SpoofClass&gt; 类型,而不是 SpoofClass 类型。

    我只能猜测您要做什么,但以下代码编译:

    Dictionary<int, SpoofClass> cart = new Dictionary<int, SpoofClass>();
    int i=0;
    foreach (var item in dbContext.DBView)
    {
        cart.Add(i, new SpoofClass { biff = item.biff });
        ++i;
    }
    

    【讨论】:

      【解决方案2】:

      您需要访问给定键的字典值。类似的东西。

      foreach(var item in dbContext.DBView)
      {
          foreach(var key in cart.Keys)
          {
              cart[key].biff = item.biff;
          }
      }
      

      【讨论】:

      • 该代码无法编译,因为 cart[key] 的类型是 SpoofClass 而 item.biff 是 int。
      • @Dylan:现在您的代码可以编译,但没有任何意义 :) 循环之后,字典中的所有条目都将具有 DBView 中最后一项的 biff 值。
      • @Daniel,虽然你是对的,但看起来这就是 OP 想要做的 ;-)
      猜你喜欢
      • 2015-07-27
      • 1970-01-01
      • 1970-01-01
      • 2013-01-09
      • 2018-09-24
      • 1970-01-01
      • 2023-03-13
      • 2014-03-14
      相关资源
      最近更新 更多