I'm writing an app which uses the Roslyn API to extract information about symbols defined in C# source code files. It does this by compiling the source code in memory and then scanning it. To make that work I have to load the assemblies and nuget packages referenced by the source code.
That was working well...until I started analyzing some source code containing some simple Linq expressions like this:
return Channels.Aggregate( retVal, ( current, channel ) => current | channel.Channel );
The details of what that code does aren't important. But having that line in the source code generated the following diagnostic error when the code base was compiled in memory:
CS1061 - 'List' does not contain a definition for 'Aggregate' and no accessible extension method 'Aggregate' accepting a first argument of type 'List' could be found (are you missing a using directive or an assembly reference?)
Channels
is a List<>
. Clearly I am missing a reference :).
What's confusing me is that there are certain default assemblies and packages I load for all projects:
okay &= TryAddMetadataReference( "netstandard", result );
okay &= TryAddMetadataReference( "System.Private.CoreLib", result );
okay &= TryAddMetadataReference( "System.Private.Uri", result );
okay &= TryAddMetadataReference( "System.Runtime.Extensions", result );
Again, the details of what this code does aren't, I think, significant. All of these calls succeed, meaning that all the targeted assemblies are found and loaded into the compilation environment.
Yet that Linq expression is getting rejected because the Linq code can't be found.
Is there another Linq-specific assembly I need to add?
According to .NET Core sources (
Aggregate
method,AssemblyInfo.cs
andSystem.Linq.csproj
files), all methods are defined inSystem.Linq
assembly (but in .NET Framework they are inSystem.Core
assembly).So, try to add and load
System.Linq
assembly