【问题标题】:Cannot find extension method找不到扩展方法
【发布时间】:2012-07-18 00:57:45
【问题描述】:

我有 ASP.net 网站。在App_Code 文件夹中,我有具有扩展方法的类。从 Visual Studio 运行此站点时,一切正常。但是在我的 IIS 7 服务器上,我收到以下错误:

CS1061:“System.Data.SqlClient.SqlDataReader”不包含“SafeGetString”的定义,并且找不到接受“System.Data.SqlClient.SqlDataReader”类型的第一个参数的扩展方法“SafeGetString”(是您缺少 using 指令或程序集引用?)

但包含此扩展方法的文件位于 App_Code 文件夹中。我错过了什么重要的东西吗?

ExtentionMethods 类:

public static class ExtentionMethods
{
    public static string SafeGetString(this SqlDataReader reader, int colIndex)
    {
        if (!reader.IsDBNull(colIndex))
            return reader.GetString(colIndex);
        return "NULL VALUE";
    }

    public static int SafeGetInt32(this SqlDataReader reader, int colIndex)
    {
        if (!reader.IsDBNull(colIndex))
            return reader.GetInt32(colIndex);
        return -1;
    }

    public static DateTime SafeGetDateTime(this SqlDataReader reader, int colIndex)
    {
        if (!reader.IsDBNull(colIndex))
        {
            try
            {
            }
            catch
            {
                return new DateTime(1800, 1, 1);
            }
        }
        return new DateTime(1800, 1, 1);
    }
}

【问题讨论】:

  • 是app_code文件夹的父文件夹还是子目录?
  • 您可能遗漏了一件重要的事情,那就是最好使用 Web 应用程序项目而不是网站。在许多情况下,网站的行为都很奇怪,也许包括这个。
  • @seeker:那么这可能会有所帮助:stackoverflow.com/questions/155105/…
  • @TimSchmelter 谢谢!为我工作。

标签: c# asp.net iis


【解决方案1】:

感谢@AnthonyWJones:How come classes in subfolders in my App_Code folder are not being found correctly?

您需要将 codeSubDirectories 添加到 web.config 中的编译元素

<configuration>
    <system.web>
      <compilation>
         <codeSubDirectories>
           <add directoryName="Extensions"/>
         </codeSubDirectories>
      </compilation>
   </system.web>
</configuration>

【讨论】:

    【解决方案2】:

    您是否在扩展方法参数中添加了“this”?类和方法是公共静态的吗?没有看到代码,我只是在这里暗中刺伤。

    如果我没记错的话,你的第一个参数必须是被扩展的类型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-23
      相关资源
      最近更新 更多