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);
}
}
}