CodeDomProvider objCodeCompiler = CodeDomProvider.CreateProvider( "CSharp" );
CompilerParameters objCompilerParameters = new CompilerParameters();
...
CompilerResults objCompileResults = objCodeCompiler.CompileAssemblyFromFile( objCompilerParameters, files.ToArray() );
Run Code Online (Sandbox Code Playgroud)
当我编译我的文件时,我得到:
FileFunctions.cs(347):错误:意外字符'$'
有没有人知道如何使用CodeDom编译进行字符串插值?
我找到了这个链接:如何使用CSharpCodeProvider定位.net 4.5?
所以我尝试过:
var providerOptions = new Dictionary<string, string>();
providerOptions.Add( "CompilerVersion", "v4.0" );
// Instantiate the compiler.
CodeDomProvider objCodeCompiler = CodeDomProvider.CreateProvider( "CSharp", providerOptions );
Run Code Online (Sandbox Code Playgroud)
但我仍然得到同样的错误.
我还将目标框架更新到.NET Framework 4.6.
注意:我不能指定"v4.5"或"v4.6"或者我会得到:
************** Exception Text **************
System.InvalidOperationException: Compiler executable file csc.exe cannot be found.
at System.CodeDom.Compiler.RedistVersionInfo.GetCompilerPath(IDictionary`2 provOptions, String compilerExecutable)
at Microsoft.CSharp.CSharpCodeGenerator.FromFileBatch(CompilerParameters options, String[] fileNames)
at Microsoft.CSharp.CSharpCodeGenerator.System.CodeDom.Compiler.ICodeCompiler.CompileAssemblyFromFileBatch(CompilerParameters options, String[] fileNames)
at …Run Code Online (Sandbox Code Playgroud) 我能够使用一个简单的c#函数,但是当我介绍一些更复杂的东西时,如下面的内容,我会遇到语法错误,并且没有很多关于如何执行此操作的示例.
我已根据此处收到的建议对代码进行了更新,但此代码仍然无法正常运行
cls
$dagDistribution = $null;
$distribution =
@'
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Security.Principal;
namespace MultiThreading
{
public class dagDistribution
{
public List<string> get(string dag)
{
DateTime start = DateTime.Now;
var response = new ConcurrentBag<Collection<PSObject>>();
var exceptions = new ConcurrentQueue<Exception>();
string dagName = "hqdag1";
string[] serversUnsorted = getDagMembers(dagName);
var servers = …Run Code Online (Sandbox Code Playgroud)