If I reference an internal
class from inside an MVC view, the AssemblyBuilder
complains that it can't access the class (which makes sense) here:
System.Web.Compilation.AssemblyBuilder.Compile()
I've tried adding InternalsVisibleTo
attributes for:
System
System.Web
System.Core
but I can't get it to work. Is this possible somehow?
Demo:
internal static class InternalClass
{
public static string Foo = "bar";
}
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
<%: InternalClass.Foo %>
Stack trace:
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)
Edit: Please don't suggest alternative ways of doing it, that isn't what im asking.
Thanks!
That's ASPX view engine, right? AFAIK it generates some code and dynamically compiles it into some randomly named assembly in Temporary Asp.Net Files folder. So, that means you are out of luck with InternalsVisibleTo attribute.
You may have some luck if you pre-compile your views e.g. Compile Views in ASP.NET MVC or Can I precompile my ASP.NET MVC application?