找不到扩展方法

时间:2012-07-18 21:34:13

标签: c# asp.net iis

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

  

CS1061:'System.Data.SqlClient.SqlDataReader'不包含'SafeGetString'的定义,并且没有可以找到接受类型'System.Data.SqlClient.SqlDataReader'的第一个参数的扩展方法'SafeGetString'(是你错过了使用指令或程序集引用?)

但是包含此扩展方法的文件位于App_Code文件夹中。我错过了一些重要的事情吗?

ExtentionMethods Class:

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

2 个答案:

答案 0 :(得分:2)

感谢@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>

答案 1 :(得分:0)

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

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

相关问题