【问题标题】:C# web project complaining about a classC# web 项目抱怨一个类
【发布时间】:2012-05-02 01:49:13
【问题描述】:

我创建了一个新项目(C# 中的 Web 项目)。 在我的项目中创建了一个名为 App_Code 的新文件夹。 然后,我继续添加一个具有以下内容的类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Shipper.DataLayer;

namespace Shipper.BusinessLayer
{
    public static class BL
    {
        public static int JustSomeTest()
        {
            return 1;
        }
    }
}

在我的一个页面default.aspx 后面的代码中我试图做:

int i = BL.JustSomeTest();

当我输入 BL. 时,我没有得到任何智能感知。它说我缺少一个程序集或参考。或者它会说The name BL does not exist in the current context。 但是如果类文件在同一个项目中,我是否必须包含参考?我什至尝试构建我的项目,它首先生成了一个 dll 文件,其名称为我的解决方案文件 Shipper.dll,但一旦我添加此引用,它就会显示为 The name BL does not exist in the current context

在我的 default.aspx 页面中,我尝试添加一条 using 语句

using Shipper.

但只要我这样做,我的命名空间 BusinessLayer 就不会显示... 我很困惑?

编辑

这里是default.aspx:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Shipper.BusinessLayer;

namespace Shipper
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                SetDefaults();
                int i = BL.JustSomeTest();
            }
        }
   }
}

这是我的BL.cs 文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Shipper.DataLayer;

namespace Shipper.BusinessLayer
{
    public static class BL
    {
        public static int JustSomeTest()
        {
            return 1;
        } 
    }
}

错误为The type or namespace nameBusinessLayerdoes not exist in the namespace Shipper (are you missing an assembly reference)?

【问题讨论】:

  • 你的方法是在类之外定义的。
  • 你能用aspx中的调用代码更新你的答案吗?
  • @Shedal 这是我的一个错误,我再次编辑了问题。
  • 您是否尝试过清除解决方案并全部重建?
  • @Steve 清除解决方案是什么意思。我已经完成了构建->清洁解决方案和构建->构建解决方案。如果我在命名空间的 app_code 文件夹中有一个类,是否需要将其添加为参考?

标签: c# asp.net


【解决方案1】:

你的……

public static int JustSomeTest()
 {
   return 1;
 }

...是out of the 'class' - 你不能只为命名空间定义方法。

(至少从您的示例来看,这可能只是一个错字,但您需要给我们一个工作示例)

【讨论】:

【解决方案2】:

你的方法在一个类之外。那是不行的。试试这个:

namespace Shipper.BusinessLayer
{
    public static class BL
    {
        public static int JustSomeTest()
        {
            return 1;
        }
    }
}

此外,如果您希望 BL 弹出,请确保引用了命名空间 (using Shipper.BusinessLayer)。但是,为什么这是一个静态类?我认为你可能不希望这样,除非你正在制作扩展方法。

【讨论】:

  • 当我构建它时说命名空间名称 BusinessLayer 的类型在命名空间托运人中不存在(您是否缺少程序集引用)?
  • 这是在另一个程序集中吗?你引用过那个程序集吗?
  • 查看我的编辑 Jeremy。这个 BL.cs 文件在同一个项目中,它是一个解决方案。 BL.cs 文件位于名为 App_Code 的文件夹中。
  • 如果你让类不是静态的?这有什么区别吗?
  • 我一直说如果我这样做 using Shipper.BusinessLayer 我从 vs 返回的消息是`名称空间 Shipper 中不存在类型或名称空间 BusinessLayer(您是否缺少程序集引用)。
【解决方案3】:

尝试删除命名空间声明:

using System;

public static class BL
{
    public static int JustSomeTest()
    {
       return 1;
    }
}

【讨论】:

    【解决方案4】:

    This forum thread on Wrox 可能也有用。

    【讨论】:

      【解决方案5】:

      试试

      使用 Shipper.BusinessLayer;

      要尝试的事情

      1. 通过在解决方案资源管理器中查看目标项目的属性来验证命名空间是否为您认为的名称。
      2. 在 Visual Studio 中使用对象浏览器并定位命名空间并验证可以看到类。如果对象浏览器看不到,代码也看不到。

      【讨论】:

      • 这是我的问题,它没有看到 BusinessLayer。
      • 通过 Visual Studio 中的项目属性验证另一个项目的命名空间。验证它们是否符合您的想法。如果您在 Web 项目中引用了该项目,请在对象浏览器中浏览到它并验证您是否看到了命名空间。
      【解决方案6】:

      首先要确保您添加了对 Shipper 项目的引用,而不是 Shipper DLL。

      然后确保重新构建 Shipper 项目,最好进行清理和构建。

      然后再次检查您的智能感知,我怀疑您引用的是更旧版本的 Shipper dll。

      【讨论】:

        【解决方案7】:

        试着让你的类不是静态的,但让你的方法保持静态。 (只是因为我从不使用静态类,但它似乎没有任何区别)

        或者,如果您遇到严重问题并且无法解决,请安装 Resharper 的副本,它可能会有所帮助!

        【讨论】:

          猜你喜欢
          • 2016-01-08
          • 1970-01-01
          • 1970-01-01
          • 2023-02-10
          • 2021-11-30
          • 1970-01-01
          • 2018-06-13
          • 2011-01-08
          • 1970-01-01
          相关资源
          最近更新 更多