Search code examples
c#.netcompilationroslynmscorlib

Roslyn compilation: type is defined in an assembly that is not referenced


i try to compile some code with Roslyn, but get the following error message:

CS0012: The type 'Func<,>' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

I'm, still wondering about the message, because Func<, > should be in mscorelib and not in System.Runtime. I've searched for this bug and only find a hot fix which should help, but does not.

Does any one had similar issues with .net 4.5.1 and the newest Roslyn version?

Thank you!


Solution

  • Ok, found a solution. System.Runtime seems to be the problem (at the beginning i thought is't a problem with protable libs).

    I need to use the following code snippet:

     List<PortableExecutableReference> refs = new List<PortableExecutableReference>();
     refs.Add(MetadataReference.CreateFromFile(Path.Combine(assemblyPath, "mscorlib.dll")));
     refs.Add(MetadataReference.CreateFromFile(Path.Combine(assemblyPath, "System.dll")));
     refs.Add(MetadataReference.CreateFromFile(Path.Combine(assemblyPath, "System.Core.dll")));
     refs.Add(MetadataReference.CreateFromFile(Path.Combine(assemblyPath, "System.Runtime.dll")));
     refs.Add(MetadataReference.CreateFromFile(Assembly.GetEntryAssembly().Location));