从文本运行时编译 class 时使用 ValueTuple
Using ValueTuple while compiling a class from text runtime
我正在尝试在运行时从文本编译 class。我的问题是我的 class 在函数 (AllLines) 中使用了 valueTupe,并且在使用此代码
时收到错误 "C:\xxxx.cs(19,28): error CS0570: 'BaseClass.AllLines' is not supported by the language"
CodeDomProvider objCodeCompiler = new Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider();
CompilerParameters objCompilerParameters = new CompilerParameters();
objCompilerParameters.ReferencedAssemblies.Add("mscorlib.dll");
objCompilerParameters.ReferencedAssemblies.Add("System.IO.dll");
objCompilerParameters.ReferencedAssemblies.Add("System.Linq.dll");
CompilerResults objCompileResults = objCodeCompiler.CompileAssemblyFromFile(objCompilerParameters, filename);
编辑:
文本文件如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
namespace MyNamespace
{
public abstract class BaseClass
{
public List<(int LineNumber, string Value)> AllLines
{
...
}
}
}
我正在使用 Microsoft.CodeDom.Providers.DotNetCompilerPlatform v2.0.0.0,
Microsoft (R) Visual C# 编译器版本 1.0.0.50618
不确定这是否是 roslyn 的实际版本。
首先,您使用 Roslyn 是正确的,因为您使用的是来自 NuGet 包 Microsoft.CodeDom.Providers.DotNetCompilerPlatform.
的 Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider
但是,您面临的问题是您的文本文件不包含有效的 C#。
- 您的
List<T>
声明在将类型参数括在括号中时无效
- 您正在向类型参数声明 (LineNumber, Value) 添加名称 (?)。
- 当
List<T>
只接受一个类型参数时,您提供了两个类型参数。 (也许你打算使用 Dictionary<TKey, TValue>
)
- 你的 属性 声明没有正文
尝试将您的文本文件替换为:
using System;
using System.Collections.Generic;
using System.Linq;
namespace MyNamespace
{
public abstract class BaseClass
{
public Dictionary<int, string> AllLines
{
get; set;
}
}
}
请注意,对于此示例,您实际上不需要 using System
或 using System.Linq
。另请注意,您不需要为此使用 Roslyn。老式的 CodeDOM 可以编译它(将 Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider
替换为 Microsoft.CSharp.CSharpCodeProvider
)。
我正在尝试在运行时从文本编译 class。我的问题是我的 class 在函数 (AllLines) 中使用了 valueTupe,并且在使用此代码
时收到错误 "C:\xxxx.cs(19,28): error CS0570: 'BaseClass.AllLines' is not supported by the language"CodeDomProvider objCodeCompiler = new Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider();
CompilerParameters objCompilerParameters = new CompilerParameters();
objCompilerParameters.ReferencedAssemblies.Add("mscorlib.dll");
objCompilerParameters.ReferencedAssemblies.Add("System.IO.dll");
objCompilerParameters.ReferencedAssemblies.Add("System.Linq.dll");
CompilerResults objCompileResults = objCodeCompiler.CompileAssemblyFromFile(objCompilerParameters, filename);
编辑:
文本文件如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
namespace MyNamespace
{
public abstract class BaseClass
{
public List<(int LineNumber, string Value)> AllLines
{
...
}
}
}
我正在使用 Microsoft.CodeDom.Providers.DotNetCompilerPlatform v2.0.0.0, Microsoft (R) Visual C# 编译器版本 1.0.0.50618
不确定这是否是 roslyn 的实际版本。
首先,您使用 Roslyn 是正确的,因为您使用的是来自 NuGet 包 Microsoft.CodeDom.Providers.DotNetCompilerPlatform.
的Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider
但是,您面临的问题是您的文本文件不包含有效的 C#。
- 您的
List<T>
声明在将类型参数括在括号中时无效 - 您正在向类型参数声明 (LineNumber, Value) 添加名称 (?)。
- 当
List<T>
只接受一个类型参数时,您提供了两个类型参数。 (也许你打算使用Dictionary<TKey, TValue>
) - 你的 属性 声明没有正文
尝试将您的文本文件替换为:
using System;
using System.Collections.Generic;
using System.Linq;
namespace MyNamespace
{
public abstract class BaseClass
{
public Dictionary<int, string> AllLines
{
get; set;
}
}
}
请注意,对于此示例,您实际上不需要 using System
或 using System.Linq
。另请注意,您不需要为此使用 Roslyn。老式的 CodeDOM 可以编译它(将 Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider
替换为 Microsoft.CSharp.CSharpCodeProvider
)。