在剃刀视图中使用内部类

时间:2014-08-27 06:55:14

标签: c# asp.net-mvc-4 razor

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

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

[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"]));
        }
    }
  }
}

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

@Properties.UIMessagesSettings.Default.GetType()

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

1 个答案:

答案 0 :(得分:6)

您应该公开您的课程或在同一个程序集中编译您的视图

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

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

如果你真的需要它,你应该尽可能改变发电机