1

I want to use Tuple in dynamically compiled code on .net 4.6.1 . Below is a minimum test case. Any help would be appreciated. Thanks!

// First, install System.ValueTuple 4.5 package via nuget into test Project.
using System;
using System.CodeDom.Compiler;

public class Program
{
    public static void Main()
    {
        string source = @"
using System;
namespace TupleTest {
    class TupleTest {
        public void test(){
            (int key, string value) tuple1 = (1, ""ok"");
        }
    }
}";
        var cp = new CompilerParameters();
        cp.GenerateExecutable = false;
        cp.GenerateInMemory = true;
        cp.IncludeDebugInformation = false;
        // cp.IncludeDebugInformation = true;
        cp.WarningLevel = 1;
        cp.TreatWarningsAsErrors = true;
        cp.ReferencedAssemblies.Add("System.dll");
        cp.ReferencedAssemblies.Add("System.ValueTuple.dll");
        var compiler = Microsoft.CSharp.CSharpCodeProvider.CreateProvider("CSharp");
        var compilerResults = compiler.CompileAssemblyFromSource(cp, source);

        if (compilerResults.Errors.HasErrors)
        {
            Console.WriteLine("Compile Failed: ");
            foreach (var error in compilerResults.Errors)
                Console.WriteLine("Line {0}, Column {1}: error {2}: {3}",
                                  error.Line, error.Column, error.ErrorNumber, error.ErrorText);
        }
        else
        {
            Console.WriteLine("Compile Pass");
        }
    }
}

Wasted a day on this, still can't get through.

Always the same error, it doesn't recognize a tuple at all.

Btw, the test code can't be test in online codefiddling for appreant security reasons, needs a local env. to verify.

Line 6, column 37: error CS1003: Syntax error, '=>' expected
Line 6, column 47: error CS1026: ) expected
Line 6, column 47: error CS1525: Invalid expression term ','
Line 6, column 49: error CS1002: ; expected
Line 6, column 53: error CS1002: ; expected
Line 6, column 53: error CS1525: Invalid expression term ')'

also tried replacing cp.ReferencedAssemblies.Add("System.ValueTuple.dll"); with cp.ReferencedAssemblies.Add("C:\\nugetpkg\\System.ValueTuple.dll"); , no avail. Aslo double add give

Line 0, column 0: error CS1703: 
An assembly with the same identity 'System.ValueTuple, Version=4.0.3.0, 
Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' 
has already been imported. Try removing one of the duplicate references.

Thanks for helping. Solved using the roslyn based compiler mentioned in answer. PS: All reference to System.ValueTuple needs to be removed from CompilerParameter.ReferencedAssemblies to avoid Multiple Reference error.

Ben
  • 866
  • 10
  • 26

1 Answers1

2

You are using language features from a later version of C#. If you do this:

        string source = @"
using System;
namespace TupleTest {
    class TupleTest {
        public void test(){
            var tuple1 = ValueTuple.Create(1, ""ok"");
            Console.WriteLine(tuple1.Item2);
        }
    }
}";

Then it will compile just fine.

I am pretty sure CSharpCodeProvider only works with version 5 or 6 of the C# language. Maybe this answer will help you get to version 7.

Take a look at this chart on how framework versions are related to language versions.

Andy
  • 10,110
  • 3
  • 28
  • 47
  • I can confirm, this works. – zaggler Feb 24 '21 at 14:45
  • I'm working on a class lib, the main program targets 4.6.1. I want to use the NuGet package System.ValueTuple to enable tuple. Thus I gotta stick with 4.6 for sometime ... – Ben Feb 24 '21 at 14:51
  • 2
    @Ben -- this has *nothing* to do with the Framework of the project you are doing. This has everything to do with the version of the C# language you are using. They are completely unrelated things. – Andy Feb 24 '21 at 14:52
  • @Andy also the version that the code provider works with must be ISO-1, ISO-2, 3, 4, 5 or Default – zaggler Feb 24 '21 at 14:59
  • @Andy, not sure what do you mean. All other componets of the program are set to target 4.6.1. I am not sure if targeting a higher version will cause any side effect. I didn't read most of the other parts. I am just altering a small part of it to enable tuple. – Ben Feb 24 '21 at 15:00
  • I dont want to bump the whole projects to target .net 4.7/C#7. But getting the NuGet package to work. Thanks. – Ben Feb 24 '21 at 15:03
  • 2
    It's not about the nuget package, the compiler that you're using through the CSharpCodeProvider doesn't understand the syntax. If CSharpCodeProvider cannot be configured to use a newer compiler you simply cannot do this. A nuget package cannot add syntax to the compiler. – Lasse V. Karlsen Feb 24 '21 at 15:05
  • See [this question](https://stackoverflow.com/questions/31639602/using-c-sharp-6-features-with-codedomprovider-roslyn) for some information on using the newer compilers. – Lasse V. Karlsen Feb 24 '21 at 15:06
  • 1
    @LasseV.Karlsen, thank you. I guess I got what you guys mean now. The language spec version (souorce text) => compiler version ==targets==> assmbely implimentations version. In deed I need a new compiler. – Ben Feb 24 '21 at 15:14