【发布时间】:2011-11-15 21:47:16
【问题描述】:
【问题讨论】:
-
请问,你为什么需要这个?
-
因为我需要在运行时创建一个.DLL并添加到项目模板中
【问题讨论】:
我不知道这是否是您要查找的内容,但这里有一篇文章解释了如何以编程方式编译代码:
http://support.microsoft.com/kb/304655
以下是那篇文章中的一些相关代码:
using System.CodeDom.Compiler;
using System.Diagnostics;
using Microsoft.CSharp;
private void button1_Click(object sender, System.EventArgs e)
{
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
ICodeCompiler icc = codeProvider.CreateCompiler();
string Output = "Out.exe";
Button ButtonObject = (Button)sender;
textBox2.Text = "";
System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
//Make sure we generate an EXE, not a DLL
parameters.GenerateExecutable = true;
parameters.OutputAssembly = Output;
CompilerResults results = icc.CompileAssemblyFromSource(parameters, textBox1.Text);
if (results.Errors.Count > 0)
{
textBox2.ForeColor = Color.Red;
foreach (CompilerError CompErr in results.Errors)
{
textBox2.Text = textBox2.Text +
"Line number " + CompErr.Line +
", Error Number: " + CompErr.ErrorNumber +
", '" + CompErr.ErrorText + ";" +
Environment.NewLine + Environment.NewLine;
}
}
else
{
//Successful Compile
textBox2.ForeColor = Color.Blue;
textBox2.Text = "Success!";
//If we clicked run then launch our EXE
if (ButtonObject.Text == "Run") Process.Start(Output);
}
}
【讨论】:
很长一段时间以来,构建 .NET 解决方案和项目的方法是使用MSBuild。
项目和解决方案文件是 MSBuild 文件。
【讨论】:
您可以使用 MSBuild 构建应用程序并将项目作为参数传递:
MSBuild.exe MyProj.proj /property:Configuration=Debug
您可以使用 System.Diagnostics.Process 从 C# 启动此过程
【讨论】: