Generating DLL assembly dynamically at run time
Asked Answered
N

4

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.

Nasya answered 2/3, 2009 at 23:30 Comment(0)
U
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

Unilingual answered 2/3, 2009 at 23:34 Comment(1)
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.Retrieve
F
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));
    }
}
Fireplace answered 16/4, 2012 at 21:28 Comment(1)
If you use parameters.GenerateInMemory = true; you can get the in-memory assembly with r.CompiledAssemblyStreamlet
O
5

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

Obala answered 2/3, 2009 at 23:35 Comment(2)
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.Bergmann
@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 assemblyObala
H
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.

Hindoo answered 14/11, 2022 at 10:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.