사용자가 코드를 입력하고 버튼 클릭 이벤트 코드가 런타임에 컴파일되는 작은 창 응용 프로그램을 사용합니다.런타임 코드 컴파일 오류가 발생합니다 - 프로세스가 파일에 액세스 할 수 없습니다
첫 번째 단추를 클릭하면 제대로 작동하지만 두 번 이상 같은 단추를 클릭하면 오류가 발생합니다 "다른 프로세스에서이 프로세스를 사용 중이기 때문에 프로세스가 Exmaple.pdb 파일에 액세스 할 수 없습니다.". 다음은 예제 코드입니다.
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Reflection;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var csc = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v3.5" } });
var parameters = new CompilerParameters(new[] { "mscorlib.dll", "System.Core.dll" }, "Example" + ".exe", true); //iloop.ToString() +
parameters.GenerateExecutable = true;
CompilerResults results = csc.CompileAssemblyFromSource(parameters,
@"using System.Linq;
class Program {
public static void Main(string[] args) {}
public static string Main1(int abc) {" + textBox1.Text.ToString()
+ @"
}
}");
results.Errors.Cast<CompilerError>().ToList().ForEach(error => Error = error.ErrorText.ToString());
var scriptClass = results.CompiledAssembly.GetType("Program");
var scriptMethod1 = scriptClass.GetMethod("Main1", BindingFlags.Static | BindingFlags.Public);
StringBuilder st = new StringBuilder(scriptMethod1.Invoke(null, new object[] { 10 }).ToString());
result = Convert.ToBoolean(st.ToString());
}
}
}
이 문제를 해결하면 동일한 버튼을 두 번 이상 누르면 제대로 작동합니다.
감사,
당신이 컴파일 때마다 디버그 정보가 포함 된 Examples.pdb 파일을 생성하도록 디버그 설정으로, 각 버튼 프레스에 Example.exe를 컴파일하는 것 같습니다
단서 .. 이러한 문제를 해결하기 위하여 ?? – sia