【问题标题】:Inaccessible public classes无法访问的公共课程
【发布时间】:2013-06-12 11:15:54
【问题描述】:

我正在使用公共 LoginContext 类来管理我的网络应用程序中的用户登录。

不幸的是,即使我公开声明了 LoginContext 类,我在 Login.aspx.cs 的部分类 Login 似乎无法访问它。

我的代码如下:

// ~/App_Code/LoginContext.cs
namespace stman
{
    public class LoginContext
    {

    }
}

// ~/Login.aspx.cs
namespace stman
{
    public partial class Login : System.Web.UI.Page
    {
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            LoginContext log = new LoginContext(); // error is here
        }
    }
}

在我实例化 LoginContext 的那一行出现的错误如下:

找不到类型或命名空间名称“LoginContext”(您是否缺少 using 指令或程序集引用?)

当我尝试为 LoginContext 生成一个新类时,它会进入 Web 应用程序的根文件夹,在那里它无法再访问我在 LoginContext 中需要的公共数据库类。

我不知道是什么导致了所有这些问题,但根据我过去 18 个月的专业知识,它们现在不应该存在......

有人可以帮忙清理这里的东西吗?具体我想知道:

  1. 我做错了什么
  2. 为什么会出错
  3. 谁能解决?

提前致谢!

编辑
我看了看,似乎 ~/App_Code/Database.cs 中的 Database 类或 ~/App_Code/LoginContext.cs 中的 LoginContext 类都无法访问页面 - 或网站中的任何页面。

【问题讨论】:

  • 尝试添加 LoginContext 类的命名空间
  • 如果检查 LoginContext.cs 属性,它是否标记为 BuildAction = Compile ?
  • @Nag 没有帮助。 LoginContext 似乎不是命名空间的一部分,尽管我可以清楚地看到它是
  • @OndrejSvejdar BuildAction for LoginContext.cs 是 Content
  • 切换到编译。

标签: c# asp.net class inheritance


【解决方案1】:

在 LoginContext.cs 属性中,将其标记为 BuildAction = Compile。

【讨论】:

  • 还必须为 Database.cs 执行此操作...我不确定这到底发生了什么变化,但它确实解决了我的问题
【解决方案2】:

当这些类位于不同的项目中时,您可以实现此行为。

如果是这样,那么您应该使用从项目名称开始的类的完整路径 ProjectName.stman.LoginContext

【讨论】:

    【解决方案3】:

    尝试使用构造函数

     namespace stman
    {
    // Database is a class that handles the sql queries and such
    public class LoginContext : Database
    {
        public LoginContext () : base("Name=LoginContext")
        {
    
        }
    }
    }
    

    【讨论】:

    • 没用,在base("Name=LoginContext") 下出现错误'object' does not contain a constructor that contains 1 arguments.。查看了我的代码并意识到我根本不需要继承数据库
    【解决方案4】:

    根据我从 App_Code 中读到的内容,网站项目和 Web 应用程序中的行为可能会有所不同。我想知道你在做什么样的项目?一种可能的解决方案是在 Web 应用程序项目中制作这个项目,这个 Link 可以帮助您制作这个项目:

    Source

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-23
      • 2022-10-24
      • 2016-02-13
      • 2018-07-29
      • 2013-09-11
      • 1970-01-01
      • 1970-01-01
      • 2021-04-30
      相关资源
      最近更新 更多