gpt4 book ai didi

c# - 当定义存在且构建页面有效时,构建解决方案错误类不包含 xxx 的定义

转载 作者:行者123 更新时间:2023-11-30 14:45:39 24 4
gpt4 key购买 nike

我有一个名为“Report”的类(class)在没有特定命名空间的情况下这样声明它被声明为全局命名空间。

using system;
//and other namespaces

public class Report
{
public string CheckIfReportAccess(string name)
{
//logic here
}
}

我在 webform pageload 方法中使用这样的 Report 类方法。

global::Report report = new global::Report();
if (!report.CheckIfReportAccess("xxx"))
{
//logic here.
}

问题是当我构建解决方案时发生错误

ERROR: 'Report' does not contain a definition for 'CheckIfReportAccess' and no extension method 'CheckIfReportAccess' accepting a first argument of type 'Report' could be found (are you missing a using directive or an assembly reference?).

我现在正在做的是进入菜单 Build > Build Page 并重建解决方案。解决方案构建和工作。但这是一个非常大的问题,它很耗时,有数百个此类引用,每次我构建解决方案时它都会出现,然后构建页面重建解决方案。

我正在使用 Visual Studio 2017。

编辑这是构建订单屏幕截图 enter image description here

我还检查了构建顺序依赖性。

最佳答案

您应该为您的类分配一个命名空间,而不是使用全局命名空间,例如,如果您的应用程序名为“MyApplication”,您可以创建一个名为“Reports”的文件夹,然后在其中创建您的类,如下所示:

namespace MyApplication.Reports
{
public class Report
{
public string CheckIfReportAccess(string name)
{
//logic here
}
}
}

如果您想在页面的后台代码中使用它,您应该能够像现在一样简单地引用命名空间:

using MyApplication.Reports;
[...]
protected void Page_Load(object sender, EventArgs e)
{
Report report = new Report();
report.CheckIfReportAccess("SomeReport");
}

如果你想让这个类对你的 View 可用,你有两个选择:

  1. 使用命名空间在您的 Web.config 中声明它:

    <pages>
    <namespaces>
    <add namespace="MyApplication.Reports"/>
    </namespaces>
    </pages>

然后您可以直接在您的 View 中访问该命名空间内的所有类:

<% Report report = new Report(); %>

<% if(report.CheckIfReportAccess("SomeReport") == "1") { %>
<div>
SomeReport has access
</div>
<% } %>
  1. 直接在您的 View 中导入它:

<%@ Import Namespace="MyApplication.Reports" %>

编辑

如果您不能如上所述应用 namespace 并且需要对所有类使用全局 namespace ,您可以考虑更改类名称(如果您遇到问题),例如,而不是 Report试试 MyReport看看是否能解决问题。

编辑2

如果由于项目限制(我觉得这很奇怪)而无法修改现有类,您始终可以使用所需的命名空间和名称创建一个新类,并从有问题的类继承:

namespace MyApplication.Reports
{
public class MyReport : Report
{
}
}

然后在您的 View 中使用“MyReport”并且不要忘记如上所述在您的 Web.Config 中注册命名空间。

关于c# - 当定义存在且构建页面有效时,构建解决方案错误类不包含 xxx 的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53003400/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com