2008-11-14 35 views

Trả lời

10

Để biên soạn một tập tin một cách nhanh chóng bạn sẽ cần phải làm một cái gì đó dọc theo những dòng (nơi sourcecode là một chuỗi có chứa có mã để biên dịch):

CodeDomProvider codeProvider = new CSharpCodeProvider(); 
ICodeCompiler compiler = codeProvider.CreateCompiler(); 

// add compiler parameters 
CompilerParameters compilerParams = new CompilerParameters(); 
compilerParams.CompilerOptions = "/target:library /optimize"; 
compilerParams.GenerateExecutable = false; 
compilerParams.GenerateInMemory = true;   
compilerParams.IncludeDebugInformation = false; 
compilerParams.ReferencedAssemblies.Add("mscorlib.dll"); 
compilerParams.ReferencedAssemblies.Add("System.dll"); 

// compile the code 
CompilerResults results = compiler.CompileAssemblyFromSource(compilerParams, sourceCode); 
4

Bạn có thể biên dịch nó một cách hợp lý dễ dàng bằng cách sử dụng CSharpCodeProvider. Bạn có thể tải xuống mã nguồn cho trình biên dịch đoạn trích của tôi, Snippy, từ số C# in Depth web site. Điều đó sử dụng CSharpCodeProvider, vì vậy bạn có thể sử dụng nó làm mã mẫu.

Các vấn đề liên quan