Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Can';在动态编译的代码中加载System.ValueTuple_C#_Tuples_Assembly References_Dynamic Compilation_Valuetuple - Fatal编程技术网

C# Can';在动态编译的代码中加载System.ValueTuple

C# Can';在动态编译的代码中加载System.ValueTuple,c#,tuples,assembly-references,dynamic-compilation,valuetuple,C#,Tuples,Assembly References,Dynamic Compilation,Valuetuple,我想在.NET4.6.1上的动态编译代码中使用元组。 下面是一个最小的测试用例。任何帮助都将不胜感激。谢谢 // 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

我想在.NET4.6.1上的动态编译代码中使用元组。 下面是一个最小的测试用例。任何帮助都将不胜感激。谢谢

// 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");
        }
    }
}
在这件事上浪费了一天,仍然无法通过

总是相同的错误,它根本无法识别元组

顺便说一句,出于明显的安全原因,测试代码不能在在线代码修改中进行测试,需要一个本地环境。核实

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 ')'
还尝试替换
cp.referencedsassemblies.Add(“System.ValueTuple.dll”)
使用
cp.referencedpassemblies.Add(“C:\\nugetpkg\\System.ValueTuple.dll”)
,没有用。
再加一次给

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.
谢谢你的帮助。使用答案中提到的基于roslyn的编译器解决。
PS:需要从
编译器参数中删除对
系统.ValueTuple
的所有引用。ReferencedAssemblys
可避免多个引用错误。

您使用的是C#语言功能。如果您这样做:

stringsource=@”
使用制度;
命名空间元组{
类元组{
公开无效测试(){
var tuple1=ValueTuple.Create(1,“确定”);
Console.WriteLine(tuple1.Item2);
}
}
}";
然后它就可以编译了

我很确定
CSharpCodeProvider
只适用于C语言的5或6版。也许会帮你升级到第7版


请看一下框架版本与语言版本之间的关系。

ValueTuple仅在.NET framework 4.7中可用…@Codexer,否,它是通过NuGet安装的。谢谢,忘了注意这一点。好的,是的,安装NuGet是不同的。这与库或类型无关,而是与编译器有关。编译器不理解您正在使用的语法,您无法安装nuget软件包来修复此问题,您必须使用更新的编译器版本。我可以确认,这是可行的。我正在处理类库,主程序的目标是4.6.1。我想使用NuGet包System.ValueTuple来启用tuple。因此,我必须坚持使用4.6版本…@Ben——这与您正在进行的项目的框架无关。这与您正在使用的C语言版本有关。它们是完全不相关的东西,与nuget包无关,您通过CSharpCodeProvider使用的编译器不理解语法。如果CSharpCodeProvider无法配置为使用较新的编译器,则无法执行此操作。nuget软件包无法向编译器添加语法。@LasseV.Karlsen,谢谢。我想我明白你们的意思了。语言规范版本(soource text)=>编译器版本==targets==>assmbely实现版本。事实上,我需要一个新的编译器。