ASP.Net MVC 3 - 动态编译的程序集用作自定义虚拟路径提供程序视图中的模型

发布于 2024-12-11 09:25:33 字数 1349 浏览 0 评论 0原文

我有一个设置,其中 MVC 模型类在控制器中动态构建,通过 CodeDomProvider 提供请求,如以下代码所示:

//model source is properly structured C# code
CodeDomProvider cdProvider = CodeDomProvider.CreateProvider("CSharp");

            CompilerParameters cParams = new CompilerParameters();
            cParams.GenerateInMemory = false;
            cParams.TreatWarningsAsErrors = false;
            cParams.WarningLevel = 4;
            cParams.OutputAssembly = string.Format(@"{0}\{1}", Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), modelNamespace+".dll");


            cParams.ReferencedAssemblies.AddRange(modelFrameworkReferences.ToArray());

            try
            {
                CompilerResults cResults = cdProvider.CompileAssemblyFromSource(cParams, modelSource);
             modelAssembly = cResults.CompiledAssembly;
             // omitted for brevity....
            }

这工作正常,模型被生成(并缓存以供以后使用),然后是它的实例通过 modelAssembly.CreateInstance 创建。

该项目还使用自定义 VirtualPathProvider 和 VirtualFile 实现来从某些外部存储中提供 Razor 视图标记。检索部分工作正常,但当框架编译 Razor 视图时,编译失败,因为视图引用动态生成的程序集中的类型。

检查 csc.exe 的视图编译命令行会发现,即使生成的程序集已加载到 appdomain 中(通过列出程序集进行验证)并且视图标记包含,对动态生成的库的引用也未包含在命令行中正确的 @using 指令引用生成的程序集的名称空间。

是否可以采取一些措施来确保动态生成的程序集类型被引用以进行视图编译?

我知道我可以使用动态模型方法,并且我可以让所有动态生成的模型类派生自视图可以引用的预定义(非动态)接口,但由于各种原因,我想引用动态生成的模型本身。

I have a setup where MVC model classes are being built on the fly in the controller serving the request via the CodeDomProvider as in the following code:

//model source is properly structured C# code
CodeDomProvider cdProvider = CodeDomProvider.CreateProvider("CSharp");

            CompilerParameters cParams = new CompilerParameters();
            cParams.GenerateInMemory = false;
            cParams.TreatWarningsAsErrors = false;
            cParams.WarningLevel = 4;
            cParams.OutputAssembly = string.Format(@"{0}\{1}", Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), modelNamespace+".dll");


            cParams.ReferencedAssemblies.AddRange(modelFrameworkReferences.ToArray());

            try
            {
                CompilerResults cResults = cdProvider.CompileAssemblyFromSource(cParams, modelSource);
             modelAssembly = cResults.CompiledAssembly;
             // omitted for brevity....
            }

This works fine and the model gets generated (and cached for later use) and then an instance of it is created via modelAssembly.CreateInstance.

The project also uses a custom VirtualPathProvider and VirtualFile implementation to serve Razor view mark-up out of some external storage. The retrieval part works fine, but when the Razor view is being compiled by the framework, the compilation fails because the view references a type in the dynamically generated assembly.

Reviewing the view compilation commandline to the csc.exe reveals that the reference to the dynamically generated library is not included in the command line even though the generated assembly is loaded in the appdomain (verified by listing the assemblies) and the view mark-up contains the proper @using directive referencing the namespace of the generated assembly.

Is there something that can be done to ensure that the dynamically generated assembly types are being referenced for view compilation?

I understand that I can use the dynamic model approach and that I can have all dynamically generated model classes derive from a pre-defined (non-dynamic) interface which the views can reference, but for various reasons I would like to have a reference to the dynamically generated model itself.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文