CompileAssemblyFromSource

CompileAssemblyFromSource

本文介绍了C# 反射 CompileAssemblyFromSource 慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在反射方面遇到了一些问题:

I've some problems with reflection:

在我的代码中,我从数据库动态加载 c# 代码,编译它并从该库加载一些对象.一切正常,但是 CompileAssemblyFromSource 进行的编译非常非常慢(大约 60 秒).

In my code I dynamically load c# code from database, compile it and load some object from that library. All that works, but the compilation made by CompileAssemblyFromSource is very very slow (about 60s).

我尝试使用 Visual Studio 2015 编译相同的 c# 代码,构建阶段用了不到 1 秒...

I tried to compile the same c# code with Visual Studio 2015 and the build phase took less than 1s...

怎么了?CompileAssemblyFromSource 这么慢吗?

What's wrong? Is CompileAssemblyFromSource so slow?

推荐答案

无论 GenerateInMemory 是否设置为 true,都会在 TEMP 目录中生成代码.通过将 CompilerParameters 中的 Tempfiles 属性设置为不存在的驱动器/目录,这将不会发生:

Code is generated in the TEMP directory regardless of whether GenerateInMemory is set to true. By setting the Tempfiles property in CompilerParameters to a non-existing drive/directory this will not happen:

     parameters.TempFiles = new TempFileCollection(@"B:\NonExistingDirectory");

这篇关于C# 反射 CompileAssemblyFromSource 慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 13:34