Problem: Can't use externally defined types in CSharpScript because it can't convert the object type from itself to itself due to some Assembly mismatch I guess.
I have 2 projects.
Common
using System;
namespace Common
{
public class Arguments
{
public string Text;
}
public class Output
{
public bool Success;
}
}
and
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();
}
}
}
When I run CSharpScriptingExperiment I get this error:
"(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]'"
On this line:
Output output = (Output)nextStep.RunAsync(globals: parameters).Result.ReturnValue;
Common is a .NET Standard 2.0 project.
CSharpScriptingExperiment is a .NET Core 2.2 project.
Any ideas? I've seen other people coming across similar issues but not found a solution.