P
P
Paul2016-10-25 22:58:49
C++ / C#
Paul, 2016-10-25 22:58:49

How to programmatically compile a project in C#?

The following task arose: it is necessary to compile a project (one or several .cs files) from a C# program, which is located in a certain directory. Found this solution:

System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
parameters.GenerateExecutable = true;
parameters.OutputAssembly = Output;
CompilerResults results = icc.CompileAssemblyFromSource(parameters,SourceString);

Where SourceString is a string representing the source code. Actually, you can sequentially read all files into one string variable, and then pass it to SourceString, however, maybe there are more elegant solutions or ready-made implementations? It's just embarrassing that all the sources are stuffed into one string, which is not good

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rou1997, 2016-10-26
@Rou1997

You can work directly with csc.exeit by passing it a list of files to compile, and if you need to compile VS projects, with resources, etc., then you need to look at the VS architecture, in particular MSBuild, as well as its API, which are used in plugins for VS .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question