Roslyn generated dll can’t be referenced

I’m trying to generate a .dll with Roslyns Emit-Api, which works, and references this .dll in another project, which fails.

What I’ve done so far:

This is how the dll is generated

 static void compileToDll()
 {
     string codeDll = @"
     using System;

     public class MyClass
     {
          public static void RunIt(FEE_BoolSlot slot)
          {
               Console.WriteLine(""Hello World!"");
          }
      }
 ";

var tree = CSharpSyntaxTree.ParseText(codeDll);
var root = (CompilationUnitSyntax)tree.GetRoot();

var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);
options = options.WithPlatform(Platform.AnyCpu);

var mscorlib = MetadataReference.CreateFromFile(typeof(object).Assembly.Location);
var compilation = CSharpCompilation.Create(
                      "HelloDll", 
                      options:options, 
                      references: new[] { mscorlib })
                 .AddSyntaxTrees(tree);

     var r = compilation.Emit("example.dll");
}

This is how it’s consumed. Which causes a System.IO.FileNotFoundException (oder Assembly “HelloDll” or it’s dependencies were not found)

enter image description here

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

class Main
{
    static void Main(string[] args)
    {
        MyClass2.RunIt();
        // the following line would work
        // var assembly = Assembly.LoadFile("C:\\...\\example.dll");
    }
}

My question is, how does i have to create or reference the created dll, so that i can use it without reflection?

Leave a Comment