gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 15:43:17 25 4
gpt4 key购买 nike

如果我从 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com