Search code examples
c#tuplesassembly-referencesdynamic-compilationvaluetuple

Can't load System.ValueTuple in dynamically compiled code


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.


Solution

  • 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.