4

问题:不能在 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 项目。

有任何想法吗?我见过其他人遇到类似的问题,但没有找到解决方案。

4

1 回答 1

3

得到它的工作与一个轻微的解决方法。

CSharpScript 有 2 个不同的范围。一个是函数、类等内部代码的正常 C# 范围。第二个是 CSharpScript 的一个独特功能,即静态范围。它允许变量和代码在不在类或函数中的情况下运行——有点像 REPL。

问题是,当一个外部类型对象被加载到静态作用域中时,它又应该被传递到一个接受该类型参数的函数中,来自静态作用域和正常作用域的对象表示是不兼容的。这是导致问题的中间静态范围。

所以它是这样的:

Executing Assembly -> Script Static Scope -> Script Normal Scope

这导致了上述问题。

这样做时:

Executing Assembly -> Script Normal Scope

或者

Script Normal Scope -> Executing Assembly

一切正常。

所以我可以将外部类型对象从函数返回到正在执行的程序集,但不能通过首先通过静态范围将外部类型对象传递给函数。

解决方法是在函数中接受一个object,然后将对象转换为函数内部的外部类型。

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, object arguments)
                    {
                        Arguments args = (Arguments)arguments;
                        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();
        }
    }
}

所以核心要点是,只要对象不通过方式的静态范围,事情就会按预期工作。如果对象需要通过静态作用域,请将其作为对象处理并在目标函数内部强制转换为所需的任何内容 - 脚本引擎似乎在强制转换本身时遇到问题,并且类型从根本上是冲突的。

这纯粹基于黑盒测试和调试——我希望 Roslyn 团队对此进行权衡,或者让从事内部工作的人来检查我的直觉和发现是否正确。

希望它可以帮助其他偶然发现此问题的人!

于 2019-05-03T10:48:03.640 回答