C# 找不到扩展方法
我有一个ASP.net网站。在C# 找不到扩展方法,c#,asp.net,iis,C#,Asp.net,Iis,我有一个ASP.net网站。在App\u code文件夹中,我有一个具有扩展方法的类。从visual studio运行此网站时,一切正常。但是,在IIS 7服务器上,我收到以下错误: CS1061:'System.Data.SqlClient.SqlDataReader'不包含'SafeGetString'的定义,并且找不到接受'System.Data.SqlClient.SqlDataReader'类型的第一个参数的扩展方法'SafeGetString'。(是否缺少using指令或程序集引用?
App\u code
文件夹中,我有一个具有扩展方法的类。从visual studio运行此网站时,一切正常。但是,在IIS 7服务器上,我收到以下错误:
CS1061:'System.Data.SqlClient.SqlDataReader'不包含'SafeGetString'的定义,并且找不到接受'System.Data.SqlClient.SqlDataReader'类型的第一个参数的扩展方法'SafeGetString'。(是否缺少using指令或程序集引用?)
但包含此扩展方法的文件位于App_Code文件夹中。我错过了什么重要的事情吗
扩展方法类:
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);
}
}
是否将“this”添加到扩展方法参数中?类和方法是公共静态的吗?没有看到密码,我只是在黑暗中刺伤
如果我没记错的话,您的第一个参数必须是被扩展的类型。感谢@AnthonyWJones: 您需要将CodeSubdirectory添加到web.config中的编译元素中
<configuration>
<system.web>
<compilation>
<codeSubDirectories>
<add directoryName="Extensions"/>
</codeSubDirectories>
</compilation>
</system.web>
</configuration>
它是在app_code文件夹的父文件夹中还是在子目录中?您可能缺少的一件重要事情是,最好使用Web应用程序项目而不是网站。网站在很多情况下都会表现得很奇怪,也许包括这一个。@seeker:那么这可能会有帮助:@TimSchmelter谢谢!对我有用。