c# - 在 MVC View 中引用内部类

标签 c# internals

如果我从 MVC View 中引用一个 internal 类,AssemblyBuilder 会提示它无法在此处访问该类(这是有道理的):

System.Web.Compilation.AssemblyBuilder.Compile()

我已经尝试为以下对象添加 InternalsVisibleTo 属性:

  • 系统
  • System.Web
  • System.Core

但我无法让它工作。这有可能吗?

演示:

internal static class InternalClass
{
    public static string Foo = "bar";
}

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
<%: InternalClass.Foo %>

堆栈跟踪:

Compiler Error Message: CS0122: 'InternalClass' is inaccessible due to its protection level
at System.Web.Compilation.AssemblyBuilder.Compile()
at System.Web.Compilation.BuildProvidersCompiler.PerformBuild()
at System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath virtualPath)
at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)
at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)
at System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean throwIfNotFound)
at System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath virtualPath, Type requiredBaseType, HttpContext context, Boolean allowCrossApp)

编辑:请不要提出替代方法,这不是我要问的。

谢谢!

最佳答案

那是 ASPX View 引擎,对吧?据我所知,它会生成一些代码并将其动态编译到 Temporary Asp.Net Files 文件夹中的一些随机命名的程序集中。所以,这意味着您对 InternalsVisibleTo 属性不满意。

如果你预编译你的 View ,你可能会有些运气,例如Compile Views in ASP.NET MVCCan I precompile my ASP.NET MVC application?

关于c# - 在 MVC View 中引用内部类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6870790/

相关文章:

c# - #define 的意义何在?

c# - 如果两个不同类的实例具有相同的属性,如何自动映射它们之间的值?

javascript - 如何在 ASP.NET MVC 中通过客户端验证比较 2 个值?

node.js - Node.js 的内部结构。它实际上是如何运作的

java - findLoadedClass() 返回 null

python - 为什么对实例方法的引用存储在每个实例对象中而不是类对象中?

c# - 项目中图像文件的路径

c# - 从 for 循环到数组的结果?

c++ - STL 内部 : deque implementation

c - 在 C 中初始化变量时,编译器是否将初始值存储在可执行文件中