BendEg
BendEg

Reputation: 21088

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!

Upvotes: 5

Views: 3235

Answers (3)

Sergey Kolodiy
Sergey Kolodiy

Reputation: 5899

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));

Upvotes: 6

Kevin Pilch
Kevin Pilch

Reputation: 11605

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.

Upvotes: 1

BendEg
BendEg

Reputation: 21088

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));

Upvotes: 6

Related Questions