CS1061:MyClass 不包含 X 的定义,并且找不到接受 MyClass 类型的第一个参数的扩展方法 X

CS1061: MyClass does not contain a definition for X and no extension method X accepting a first argument of type MyClass could be found

HttpParseException 错误 CS1061:'mcl959.Person' 不包含 'HasEmail' 的定义,并且找不到接受类型 'mcl959.Person' 的第一个参数的扩展方法 'HasEmail'(是您缺少 using 指令或程序集引用?)

我正在为使用 .NET Framework 4.5.2 构建的 ASP.NET 网站获取上述 Compiler Error CS1061

我明白这个错误,但我不明白为什么会这样。

这是我的 StackTrace,显示错误发生在 Site1.Master.cs 第 590 行:

c:\Development\mcl959\Site1.Master.cs(590): error CS1061: 'mcl959.Person' does not contain a definition for 'HasEmail' and no extension method 'HasEmail' accepting a first argument of type 'mcl959.Person' could be found (are you missing a using directive or an assembly reference?)
Source: System.Web
HResult: -2147467259
HelpLink:
StackTrace:
at System.Web.UI.TemplateParser.ProcessException(Exception ex)
at System.Web.UI.TemplateParser.ParseStringInternal(String text, Encoding fileEncoding)
at System.Web.UI.TemplateParser.ParseString(String text, VirtualPath virtualPath, Encoding fileEncoding)
at System.Web.UI.TemplateParser.ParseFile(String physicalPath, VirtualPath virtualPath)
at System.Web.UI.TemplateParser.ParseInternal()
at System.Web.UI.TemplateParser.Parse()
at System.Web.Compilation.BaseTemplateBuildProvider.get_CodeCompilerType() at
System.Web.Compilation.BuildProvider.GetCompilerTypeFromBuildProvider(BuildProvider buildProvider)
at System.Web.Compilation.BuildProvidersCompiler.ProcessBuildProviders()
at System.Web.Compilation.BuildProvidersCompiler.PerformBuild()
at System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath virtualPath)
at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)
at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)
at System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean throwIfNotFound) at System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath virtualPath, Type requiredBaseType, HttpContext context, Boolean allowCrossApp)
at System.Web.UI.PageHandlerFactory.GetHandlerHelper(HttpContext context, String requestType, VirtualPath virtualPath, String physicalPath)
at System.Web.UI.PageHandlerFactory.GetHandler(HttpContext context, String requestType, String virtualPath, String path)
at System.Web.HttpApplication.MaterializeHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step)
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

下面是包含第 590 行的屏幕截图,突出显示,Intelliscence 显示 HasEmail[=42 的定义=]:

下面是显示项目 mcl959 与 class Person 和我的两 (2) 个版本的 HasEmail函数(最上面的一个没用过):

CS1061错误是当您试图调用一个方法或访问一个不存在的class成员时引起的。正如我所展示的,它确实存在于我的代码中。

为什么会出现此错误?

如果您的应用程序是 ASP 网络表单站点(而不是 asp 网络表单应用程序),它将仅在运行时按需编译网页。这可能是您仅在运行时而非编译时收到错误的原因。

根据错误本身,我的猜测是 lambda 函数未在定义 class/method 的同一程序集中执行。因为它被标记为内部,所以它会抛出该错误。

阅读更多: https://docs.microsoft.com/en-us/aspnet/web-forms/overview/older-versions-getting-started/deploying-web-site-projects/precompiling-your-website-cs

https://docs.microsoft.com/pt-br/dotnet/csharp/language-reference/keywords/internal

我以前在使用 WebForms 开发时遇到过这个问题。

这仅适用于您的 Web 项目是“网站”项目而不是“Web 应用程序”项目的情况。如果它是一个 web 应用程序项目,甚至可能工作,但我怀疑它,值得一试。

“网站”项目通常在部署站点时不需要编译的 dll,或者在调试时 运行 在本地不需要。该网站即时编译。

编译后的 dll 将进入 Temporary ASP.NET Files 文件夹。如果您删除这些文件,那么它应该可以工作。可能是这些编译好的dll没有更新。

尝试清除这些文件夹中的文件

c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files

这个文件夹可能不存在,但如果它确实清除了它

%AppData%\Local\Temp\Temporary ASP.NET Files

您可能需要关闭 Visual studio,如果您使用的是 IIS,那么最好停止 IIS。

在此处阅读有关动态编译的更多信息: Understanding ASP.NET Dynamic Compilation