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.
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();
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With