简体   繁体   English

如何使用C#ScriptEngine在类中执行方法

[英]How to execute a method in class with C# ScriptEngine

How can I get the result of this source 我如何获得此来源的结果

var src = @"public class Test
{
    public IList<string> CallMe(int count = 10)
    {
        return Enumerable.Range(0, count).Select(x => $""Number-{x}"").ToList();
    }
}";

by Roslyn ScriptEngine in C#? 由Roslyn ScriptEngine在C#中编写?

How can I do this? 我怎样才能做到这一点?

object result = await CSharpScript.EvaluateAsync("new Test().CallMe(20)");

The one of way to do that is use continuation from Roslyn Scripting that keeps the previous state of execution: 一种实现方法是使用Roslyn Scripting的延续来保持先前的执行状态:

var src = @"public class Test
{
    public IList<string> CallMe(int count = 10)
    {
        return Enumerable.Range(0, count).Select(x => $""Number-{x}"").ToList();
    }
}";

var options = ScriptOptions.Default
    .AddReferences("mscorlib", "System.Core") // assume that it's run under .netframework
    .AddImports("System", "System.Linq", "System.Collections.Generic");

var list = CSharpScript.RunAsync(src, options).Result
                       .ContinueWithAsync<IList<string>>("new Test().CallMe(20)").Result.ReturnValue;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM