Reputation: 1796
Program A: output a c# function, and insert the function into a file (eg. Classifier.cs) in another c# project B. These steps have already been implemented.
I am wondering is there any way to programmatically build and compile the the c# project B inside project A. So I can click a button in project A, it will automatically insert the new function into project B, build, compile the project B. And finally launch the new project B.
Thank you.
Upvotes: 5
Views: 5439
Reputation: 1631
There are a lot of compilation assemblies for .NET that are now obsoletes. For compilation, use Microsoft.Build.
First, Add this following references :
Secondly, import :
using Microsoft.Build.Evaluation;
And finally, write this code :
var configuration = "Release";
var projectDirectory = @"C:\blabla";
var collection = ProjectCollection.GlobalProjectCollection;
var project = collection.LoadProject($@"{projectDirectory}\MyProject.csproj");
project.SetProperty("Configuration", configuration);
project.Build();
Upvotes: 3
Reputation: 3402
Assuming you're using Visual Studio you could just use run MSBuild against your ProjectB.csproj. That way you project should build identical to how it would build in Visual Studio.
Upvotes: -1
Reputation: 3621
No need to execute a new process. .NET gives us a way to invoke the C# compiler programatically. I find this technique works well:
http://blogs.msdn.com/b/dohollan/archive/2010/08/09/programmatically-invoke-the-c-compiler.aspx
Upvotes: 1