0

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?

Mark Olbert
  • 6,584
  • 9
  • 35
  • 69
  • 1
    IIRC, any `Linq` method, like [`Aggregate`](https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.aggregate?view=netframework-4.8), is defined in `System.Core` assembly, try to add it – Pavel Anikhouski Mar 28 '20 at 18:18
  • but .NET Core [sources](https://source.dot.net/#System.Linq/System.Linq.AssemblyInfo.cs) show that it's defined in a separate `System.Linq` assembly – Pavel Anikhouski Mar 28 '20 at 18:24
  • Awesome, @PavelAnikhouski! Adding System.Linq explicitly worked. Not sure why I didn't think of that myself... :) If you'd care to write it up as an answer I'd be happy to accept and upvote it. – Mark Olbert Mar 28 '20 at 18:59

1 Answers1

1

According to .NET Core sources (Aggregate method, AssemblyInfo.cs and System.Linq.csproj files), all methods are defined in System.Linq assembly (but in .NET Framework they are in System.Core assembly).

So, try to add and load System.Linq assembly

Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66
  • Thanx again, @Pavel. May I ask a follow-up? If you're familiar with project.assets.json (which I parse to get a bunch of needed info) why isn't System.Linq listed in it for a project which requires it? Alternatively, is there a manifest somewhere that details what's in netstandard.dll vs what's not but is required for netstandard? – Mark Olbert Mar 28 '20 at 19:25
  • @MarkOlbert I think, that `System.Linq` is a part of .NET Core SDK, which is used in the application. However, behavior may vary between .NET Core 2.x and 3.x – Pavel Anikhouski Mar 28 '20 at 19:40