Reputation: 9926
I have some class ( simple class ) and i want to compile in runtime this class and create ( in runtime ) some dll that will contain this class.
Is there some way to do it ?
Thanks.
Upvotes: 3
Views: 2766
Reputation: 2476
You can use the CSharpCodeProvider
to complile code at run time. See this MSDN blog.
PS. Found by a quick google search ;)
Upvotes: 1
Reputation: 1500465
Yes, use CSharpCodeProvider.
You can read the sample code for "Snippy" that I used for C# in Depth - it does exactly this sort of thing.
You can ask CSharpCodeProvider
to write to a file or build the assembly in memmory.
Sample code:
using System;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
class Test
{
static void Main()
{
var provider = new CSharpCodeProvider();
var options = new CompilerParameters {
OutputAssembly = "Foo.dll"
};
string source = "public class Foo {}";
provider.CompileAssemblyFromSource(options, new[] { source });
}
}
Upvotes: 7