由于其保护级别,“设置”无法访问

问题描述 投票:6回答:1

我在我的Utilities.CS文件夹中有以下App_Code文件作为我的MVC4应用程序中使用的“帮助器”方法(构建操作设置为编译)

如图所示,代码中有一个断点......

应用程序编译(Ctrl-Shift-B)没有错误,但是当我运行应用程序时,我会在断点后的CS0122: 'Settings' is inaccessible due to its protection level语句中得到一个return

AdminGroup设置在设置设计器中定义为public

断点线永远不会受到影响,可能是由于运行时编译错误......但是如果我编译它,为什么它会在运行时重新编译?

(对不起,我是MVC的新手,所以不确定发生了什么)

namespace MyApplication
{
    public class Utilities
    {
        public static string UserID
        {
            get
            {
                return Regex.Replace(WindowsIdentity.GetCurrent().Name, @".+\\", "").ToUpper();
            }
        }

        public static bool IsAdmin
        {
            get
            {
                System.Diagnostics.Debug.WriteLine("Break point on this line");
                return (HttpContext.Current.User.IsInRole(Properties.Settings.Default.AdminGroup));
            }
        }
    }

}

UPDATE

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.42000
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace MyApplication.Properties {


    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "14.0.0.0")]
    internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {

        private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));

        public static Settings Default {
            get {
                return defaultInstance;
            }
        }

    //
    // Other Settings Removed
    //

        [global::System.Configuration.ApplicationScopedSettingAttribute()]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.Configuration.DefaultSettingValueAttribute("MYDOMAIN\\ADMINGROUP")]
        public string AdminGroup {
            get {
                return ((string)(this["AdminGroup"]));
            }
        }
    }
}
c# asp.net-mvc class asp.net-mvc-4 access-modifiers
1个回答
17
投票

这个错误是因为Settings类是internal

我假设您已经从visual studio项目属性部分设置中创建和修改了设置。 Right click on project > Properties > Settings.有一个名为Access Modifier的下拉菜单,您需要从内部更改为公共。

enter image description here

有关内部关键字的更多信息:The internal keyword is an access modifier for types and type members. Internal types or members are accessible only within files in the same assembly

© www.soinside.com 2019 - 2024. All rights reserved.