我有 ASP.net 网站。在 App_Code
文件夹中,我有一个具有扩展方法的类。从 Visual Studio 运行此网站时,一切正常。但是在我的 IIS 7 服务器上,我收到以下错误:
CS1061: 'System.Data.SqlClient.SqlDataReader' does not contain a definition for 'SafeGetString' and no extension method 'SafeGetString' accepting a first argument of type 'System.Data.SqlClient.SqlDataReader' could be found (are you missing a using directive or an assembly reference?)
但是包含此扩展方法的文件位于 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);
}
}
最佳答案
感谢@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>
关于c# - 找不到扩展方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11550717/