1
我試着寫一個.NET的核心控制檯程序使用C#API腳本運行LINQ的聲明,但我不斷收到錯誤說C#腳本API不會加載組件
「System.Linq的,版本= 4.1 .1.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a'---> System.IO.FileLoadException:本機映像無法多次加載。
這裏是我的程序:
using System;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.CodeAnalysis.CSharp.Scripting;
using Microsoft.CodeAnalysis.Scripting;
namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
try
{
var scriptOptions = ScriptOptions.Default;
//Add reference to mscorlib
var mscorlib = typeof(System.Object).GetTypeInfo().Assembly;
var systemCore = typeof(System.Linq.Enumerable).GetTypeInfo().Assembly;
scriptOptions = scriptOptions.AddReferences(mscorlib, systemCore);
//Add namespaces
scriptOptions = scriptOptions.AddImports("System");
scriptOptions = scriptOptions.AddImports("System.Linq");
scriptOptions = scriptOptions.AddImports("System.Collections.Generic");
var state = CSharpScript.RunAsync(@"var x = new List<int>(){1,2,3,4,5};", scriptOptions).Result;
state = state.ContinueWithAsync("var y = x.Take(3).ToList();").Result;
var y = state.Variables[1];
var yList = (List<int>)y.Value;
foreach(var val in yList)
{
Console.Write(val + " "); // Prints 1 2 3
}
}
catch (CompilationErrorException e)
{
Console.WriteLine(string.Join(Environment.NewLine, e.Diagnostics));
}
catch (Exception e)
{
Console.WriteLine(string.Join(Environment.NewLine, e));
}
}
}
}
這裏是我的project.json:
{
"version": "1.0.0-*",
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.CodeAnalysis.CSharp.Scripting": "2.0.0-rc2"
},
"frameworks": {
"netcoreapp1.1": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.1.0"
}
},
"imports": ["dnxcore50", "net452"]
}
}
}
哇,它的作品〜非常感謝。附: InteractiveAssemblyLoader是一次性的,所以你可能想要使用更新這個答案。 –