I am attempting to generate a .csproj file programatically using the MSBuild
namespace, but the lack of code samples is making this a tedious and error prone process.
So far, I have got the following code but I am not sure how to add classes, references and such to the project.
public void Build()
{
string projectFile = string.Format(@"{0}\{1}.csproj", Common.GetApplicationDataFolder(), _project.Target.AssemblyName);
Microsoft.Build.Evaluation.Project p = new Microsoft.Build.Evaluation.Project();
ProjectCollection pc = new ProjectCollection();
Dictionary<string, string> globalProperty = new Dictionary<string, string>();
globalProperty.Add("Configuration", "Debug");
globalProperty.Add("Platform", "x64");
BuildRequestData buildRequest = new BuildRequestData(projectFile, globalProperty, null, new string[] { "Build" }, null);
p.Save(projectFile);
BuildResult buildResult = BuildManager.DefaultBuildManager.Build(new BuildParameters(pc), buildRequest);
}
Thanks.
Developers compile CSPROJ files using MSBuild (the Microsoft Build Engine). When developers create a new program in Microsoft Visual Studio, they start by creating a new project and associated project file.
Right-click on your project in solution explorer and select Unload Project. Right-click on the project (tagged as unavailable in solution explorer) and click "Edit yourproj. csproj". This will open up your CSPROJ file for editing.
If your program code is already in a Visual Studio project, open the project. To do so, you can double-click or tap on the . csproj file in Windows File Explorer, or choose Open a project in Visual Studio, browse to find the . csproj file, and select the file.
What is a CSProj file? Files with CSPROJ extension represent a C# project file that contains the list of files included in a project along with the references to system assemblies.
If you really want to create a proj file with MSBuild API you have to use the Microsoft.Build.Construction namespace. Most of the types you will need are in the Micrsoft.Build.dll assembly. A short sample that creates a project file with a few properties and item groups:
class Program
{
static void Main(string[] args)
{
var root = ProjectRootElement.Create();
var group = root.AddPropertyGroup();
group.AddProperty("Configuration", "Debug");
group.AddProperty("Platform", "x64");
// references
AddItems(root, "Reference", "System", "System.Core");
// items to compile
AddItems(root, "Compile", "test.cs");
var target = root.AddTarget("Build");
var task = target.AddTask("Csc");
task.SetParameter("Sources", "@(Compile)");
task.SetParameter("OutputAssembly", "test.dll");
root.Save("test.csproj");
Console.WriteLine(File.ReadAllText("test.csproj"));
}
private static void AddItems(ProjectRootElement elem, string groupName, params string[] items)
{
var group = elem.AddItemGroup();
foreach(var item in items)
{
group.AddItem(groupName, item);
}
}
}
Note that this just creates the proj file. It doesn't run it. Also, properties like "Configuration" and "Platform" are only meaningful in the context of a proj file generated by Visual Studio. Here they won't really do anything unless you add more property groups with conditions the way Visual Studio does automatically.
Like I indicated in my comments, I think this is wrong way to go about this. You really just want dynamic compilation of sources, which is available through CodeDom:
using System.CodeDom.Compiler;
class Program
{
static void Main(string[] args)
{
var provider = CodeDomProvider.CreateProvider("C#");
string[] sources = {
@"public abstract class BaseClass { public abstract void Test(); }",
@"public class CustomGenerated : BaseClass { public override void Test() { System.Console.WriteLine(""Hello World""); } }"
};
var results = provider.CompileAssemblyFromSource(new CompilerParameters() {
GenerateExecutable = false,
ReferencedAssemblies = { "System.dll", "System.Core.dll" },
IncludeDebugInformation = true,
CompilerOptions = "/platform:anycpu"
}, sources);
if (results.Errors.Count > 0)
{
Console.WriteLine("{0} errors", results.Errors.Count);
foreach(CompilerError error in results.Errors)
{
Console.WriteLine("{0}: {1}", error.ErrorNumber, error.ErrorText);
}
}
else
{
var type = results.CompiledAssembly.GetType("CustomGenerated");
object instance = Activator.CreateInstance(type);
type.GetMethod("Test").Invoke(instance, null);
}
}
}
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