35

Currently I have some code that is being generated dynamically. In other words, a C# .cs file is created dynamically by the program, and the intention is to include this C# file in another project.

The challenge is that I would like to generate a .DLL file instead of generating a C# .cs file so that it could be referenced by any kind of .NET application (not only C#) and therefore be more useful.

Eric Dand
  • 1,106
  • 13
  • 37
7wp
  • 12,505
  • 20
  • 77
  • 103

4 Answers4

42
using System.CodeDom.Compiler;
using System.Diagnostics;
using Microsoft.CSharp;

CSharpCodeProvider codeProvider = new CSharpCodeProvider();
ICodeCompiler icc = codeProvider.CreateCompiler();
System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
parameters.GenerateExecutable = false;
parameters.OutputAssembly = "AutoGen.dll";
CompilerResults results = icc.CompileAssemblyFromSource(parameters, yourCodeAsString);

Adapted from http://support.microsoft.com/kb/304655

Rex M
  • 142,167
  • 33
  • 283
  • 313
  • Note: older versions of this code have an extra line of code which create a compiler from the provider using CSharpCodeProvider.CreateCompiler(). This is deprecated, you should call compile on the provider directly. – Andrew Hill Dec 28 '17 at 22:21
36

The non-deprecated way of doing it (using .NET 4.0 as previous posters mentioned):

using System.CodeDom.Compiler;
using System.Reflection;
using System;
public class J
{
    public static void Main()
    {       
        System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
        parameters.GenerateExecutable = false;
        parameters.OutputAssembly = "AutoGen.dll";

        CompilerResults r = CodeDomProvider.CreateProvider("CSharp").CompileAssemblyFromSource(parameters, "public class B {public static int k=7;}");

        //verify generation
        Console.WriteLine(Assembly.LoadFrom("AutoGen.dll").GetType("B").GetField("k").GetValue(null));
    }
}
Lars A. Brekken
  • 24,405
  • 3
  • 25
  • 27
Anssssss
  • 3,087
  • 31
  • 40
5

Right now, your best bet is CSharpCodeProvider; the plans for 4.0 include "compiler as a service", which will make this fully managed.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • title of topic is "generating-dll-assembly-dynamically-at-run-time" NOT "generating-EXE-assembly-dynamically-at-run-time" your link description abut create exe at runtime. – Amin Ghaderi Apr 01 '15 at 08:30
  • 1
    @AminGhaderi and who said anything about exe? If you mean "but the code sample on MSDN creates an exe" - it will happily create dlls too; ultimately, the file package is *not* the interesting part of an assembly – Marc Gravell Apr 01 '15 at 08:31
0

An approach above doesn't seems work anymore (I've tried it from .Net core and .net framework 4.7.2 targeting project). I was getting "PlatformNotSupportedException" and to undestand, I've dived into source code and found this piece. And here is the explanation from them why it doesn't work and how to deal with this (link and documentation).

To be short (spoiler): I went to 1st link from there and it helped me. So, you can do it as described here: how-to-compile-a-c-sharp-file-with-roslyn-programmatically. It may be treated as an possibe answer.