问题:不能在 CSharpScript 中使用外部定义的类型,因为我猜由于某些程序集不匹配,它无法将对象类型从自身转换为自身。
我有2个项目。
常见的
using System;
namespace Common
{
public class Arguments
{
public string Text;
}
public class Output
{
public bool Success;
}
}
和
CSharpScriptingExperiment
using System;
using System.Collections.Generic;
using Common;
using Microsoft.CodeAnalysis.CSharp.Scripting;
using Microsoft.CodeAnalysis.Scripting;
public class Parameters
{
public string text;
public Arguments arguments;
}
namespace CSharpScriptingExperiment
{
class Program
{
static void Main(string[] args)
{
ScriptOptions options = ScriptOptions.Default.WithImports(new List<string>() { "Common" });
options = options.AddReferences(typeof(Arguments).Assembly);
// Script will compare the text inside arguments object to the text passed in via function parameters
var script = CSharpScript.Create(@"
public class TestClass
{
public Output DoSomething(string text, Arguments args)
{
return new Output() { Success = args.Text == text };
}
}", options: options, globalsType: typeof(Parameters));
var nextStep = script.ContinueWith<object>("return new TestClass().DoSomething(text, arguments);");
// Setup the global paramters object
Parameters parameters = new Parameters();
parameters.text = "Hello";
parameters.arguments = new Arguments()
{
Text = "Hello"
};
// Run script
Output output = (Output)nextStep.RunAsync(globals: parameters).Result.ReturnValue;
Console.WriteLine(output.Success);
Console.ReadLine();
}
}
}
当我运行CSharpScriptingExperiment时,我收到此错误:
"(1,42): error CS1503: Argument 2: cannot convert from 'Common.Arguments [/Users/username/Projects/CSharpScriptingExperiment/CSharpScriptingExperiment/bin/Debug/netcoreapp2.2/Common.dll]' to 'Common.Arguments [Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]'"
在这条线上:
Output output = (Output)nextStep.RunAsync(globals: parameters).Result.ReturnValue;
Common是一个 .NET Standard 2.0 项目。
CSharpScriptingExperiment是一个 .NET Core 2.2 项目。
有任何想法吗?我见过其他人遇到类似的问题,但没有找到解决方案。