【问题标题】:Use an internal class in razor view在剃刀视图中使用内部类
【发布时间】:2014-08-27 06:55:14
【问题描述】:

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

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

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

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

@Properties.UIMessagesSettings.Default.GetType()

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

【问题讨论】:

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


    【解决方案1】:

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

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

    如果您不在单个库中编译视图,则不能在其中使用内部类,因为运行时会将它们编译为单独的程序集,并且该程序集将看不到其他程序集的内部类

    如果你真的需要它,你应该尽可能更换生成器

    【讨论】:

    • 设置类是自动生成的,因此不能组装或公开
    • 但是为什么要使用设置文件来显示一些文本标签。我认为资源就是为此而设计的?
    • 但是资源文件也有一个内部类所以如何在razor视图中使用它
    • 如何将视图编译到同一个程序集中?
    猜你喜欢
    • 2012-07-22
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-06
    • 1970-01-01
    • 1970-01-01
    • 2018-04-24
    相关资源
    最近更新 更多