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

3

5

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!

Dissoluble answered 13/7, 2015 at 9:8 Comment(2)
What makes you think it shouldn't be in System.Runtime?Rhodian
@Rhodian it was the problem. See my answer. Thank you :)Dissoluble
D
6

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));
Dissoluble answered 13/7, 2015 at 9:37 Comment(1)
This is the right workaround for now. Note that depending on what types are mentioned in the APIs you use from the PCL you are referencing, you might need to add more facade refs than just that one.Vandyke
I
6

I had a similar issue recently. I added the following line and it solved the problem:

assemblyIdentityComparer: DesktopAssemblyIdentityComparer.Default

The whole CSharpCompilation object initialization looks like this:

var compilation = CSharpCompilation.Create(
    assemblyName,
    new[] { syntaxTree },
    references,
    new CSharpCompilationOptions(
        OutputKind.DynamicallyLinkedLibrary,
        optimizationLevel: OptimizationLevel.Release,
        assemblyIdentityComparer: DesktopAssemblyIdentityComparer.Default));
Interception answered 15/2, 2016 at 17:46 Comment(1)
You're my hero! I spent all day looking and this was the only thing that came close to workingTannen
V
1

There are a couple related bugs here, one in The MSBuild targets and one in Roslyn's MSBuildWorkspace. They should all be fixed when the RTM version of the MSBuild tools for VS2015 package and the 1.0 release of Roslyn come out.

Normally MSBuild will automatically add references to System.Runtime and the rest of the facade assemblies of you reference a portable class library via the "ImplicitlyExpandDesignTimeFacades" target, but this was broken for MSBuildWorkspace. (Note: as of 7/20/2015, this is now fixed.)

See https://github.com/dotnet/roslyn/issues/2824 for some more details.

Vandyke answered 13/7, 2015 at 12:24 Comment(1)
Never mind. I just realized that you are constructing the Compilation manually, so you should add the Facades yourself.Vandyke

© 2022 - 2024 — McMap. All rights reserved.