在剃刀视图中使用内部类

nee*_*hah 4 c# razor asp.net-mvc-4

我有一个内部类Web.Properties作为设置类Settings.settings.现在我想在mvc 4的剃刀视图(.cshtml文件)中使用其Settings类的属性

以下代码是设置文件(自动生成)

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

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

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

    [global::System.Configuration.ApplicationScopedSettingAttribute()]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [global::System.Configuration.DefaultSettingValueAttribute("An error occured while loading grid data.")]
    public string GridErrorMsg {
        get {
            return ((string)(this["GridErrorMsg"]));
        }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我想在剃刀视图(.cshtml)中使用GridErrorMsg属性我尝试使用以下代码

@Properties.UIMessagesSettings.Default.GetType()
Run Code Online (Sandbox Code Playgroud)

但它说无法访问UIMessagesSettings,因为它是一个内部类.这个问题的解决方案是什么?

小智 6

您应该将您的类设置为public,或者在同一个程序集中编译您的视图

如果您的视图是在单独的程序集中编译的,您也可以使用"内部友好" http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute(v=vs.110).aspx

如果你不在单个库中编译视图,那么就不能在那里使用内部类,因为运行时将它们编译为单独的程序集,而程序集将不会看到其他内部类

如果您真的需要它,您应该尽可能更换发电机