【问题标题】:Can't access public class enum in page_load ASP.NET无法访问 page_load ASP.NET 中的公共类枚举
【发布时间】:2014-01-26 17:07:13
【问题描述】:

我很困惑,我有以下类定义

public class User
{
    public enum UserRoleTypes : int
    {
        Guest = 0,
        User = 1,
        Administrator = 2,
        Developer = 3,
    }
}

但是,当尝试在这样的 Page_Load 方法中访问此公共枚举时。

public partial class Options_AccessControl : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        User currentUser = OCMSession.getCurrentUser(this);
        if (currentUser.getRoleID() < (int)User.UserRoleTypes.Administrator)
        {
            LogWriter logger = new LogWriter();
        }
    }
}

我收到以下错误。

Error   5   'System.Security.Principal.IPrincipal' does not contain a definition for 'UserRoleTypes' and no extension method 'UserRoleTypes' accepting a first argument of type 'System.Security.Principal.IPrincipal' could be found (are you missing a using directive or an assembly reference?) H:\SVN\OCM\Options\AccessControl.aspx.cs    15  49  OCM

但是,相同的代码在没有 User 类定义的情况下也能工作。像 Entity.UserRoleTypes 或者如果我将枚举定义移到类之外。非常感谢您对理解此错误的任何帮助。

【问题讨论】:

    标签: asp.net .net web enums


    【解决方案1】:

    您正在使用用户,但在这种情况下,这是页面的属性。它是 IPrincipal 类型,不包含您的枚举。如果您想使用新创建的类,则必须更具体或为您的类选择不同的名称。

    【讨论】:

    • 有没有办法在页面属性上指定该类?也许将其放入命名空间中。
    • @garyamorris,是的,只需使用命名空间指定更具体的路径...在我看来这很难看,但它有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-16
    • 2013-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多