3

I'm trying to use CSharpCodeProvider to compile a piece of code.

The error is:

c:\...\crust.cs(551,48) : error CS1061: 'string' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)        

The code being compiled is:

using System;
...
using System.Linq;

...

          return new string(str.Substring(1).Select(character => (char)((character - 54545) ^ key)).ToArray());
...

The code compiling the source is as follows:

...
                String exeName = String.Format("NAME.exe");

                CompilerParameters cp = new CompilerParameters
                {
                    GenerateExecutable = true,
                    GenerateInMemory = false,
                    OutputAssembly = "NAME.exe",
                    CompilerOptions = "/target:exe /platform:x64"
                };

                cp.ReferencedAssemblies.Add("System.dll");
                ...
                cp.ReferencedAssemblies.Add("System.Linq.dll");
                cp.GenerateExecutable = true;
                cp.OutputAssembly = exeName;
                cp.GenerateInMemory = false;
                cp.TreatWarningsAsErrors = false;
                CompilerResults cr = provider.CompileAssemblyFromFile(cp,
                    sourceName);
...

No other system.linq methods work either.

Notes: Compiler is 64 bit Source is being compiled in to 64 bit Compiler is in .net framework 4.0 Source is being compiled in to .net 4.0

iJynx
  • 31
  • 1
  • Have you checked what is the Linq dependency version for CSharpCodeProvider ? Maybe it is a version that does not accept the Select method with string as first param – Pedro Coelho Oct 31 '20 at 16:36

1 Answers1

1

Maybe because you haven't declared using statment for 'System.Linq' package:

using System.Linq;

Check it out in the cs file and make sure the using statment is declared explicitly.

Edward Zhang
  • 441
  • 4
  • 13