博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Scripting API Samples
阅读量:6984 次
发布时间:2019-06-27

本文共 3771 字,大约阅读时间需要 12 分钟。

 

Scripting API Samples

Tomáš Matoušek edited this page on Jan 31 ·

Pages 43

  • Contributors

    Tool Authors

    Status

    .NET Core

    Interactive and Scripting

    Clone this wiki locally

    The scripting APIs enable .NET applications to instatiate a C# engine and execute code snippets against host-supplied objects. Below are examples of how to get started with the scripting APIs and some common samples. You can also view the Scripting API .

    Supported Platforms

    Scripting APIs require desktop .NET Framework 4.6+, or .NET Core 1.1 (supported since , Visual Studio 2017 RC3).

    Scripting APIs can't be used within Universal Windows Applications and .NET Native since the application model doesn't support loading code generated at runtime.

    Getting Started

    Install the :

    Install-Package Microsoft.CodeAnalysis.CSharp.Scripting

    Code Samples

    Note: the samples require the following using:

    using Microsoft.CodeAnalysis.CSharp.Scripting;

    Scenarios

    Evaluate a C# expression

    object result = await CSharpScript.EvaluateAsync("1 + 2");

    Evaluate a C# expression (strongly-typed)

    int result = await CSharpScript.EvaluateAsync<int>("1 + 2");

    Evaluate a C# expression with error handling

    try

    {

    Console.WriteLine(await CSharpScript.EvaluateAsync("2+2"));

    }

    catch (CompilationErrorException e)

    {

    Console.WriteLine(string.Join(Environment.NewLine, e.Diagnostics));

    }

    Add references

    var result = await CSharpScript.EvaluateAsync("System.Net.Dns.GetHostName()",

    ScriptOptions.Default.WithReferences(typeof(System.Net.Dns).Assembly));

    Add imports

    var result = await CSharpScript.EvaluateAsync("Sqrt(2)",

    ScriptOptions.Default.WithImports("System.Math"));

    Parameterize a script

    public class Globals

    {

    public int X;

    public int Y;

    }

    var globals = new Globals { X = 1, Y = 2 };

    Console.WriteLine(await CSharpScript.EvaluateAsync<int>("X+Y", globals: globals));

    Note: Currently the Globals type has to be defined in an assembly loaded from a file. If the assembly is in-memory (including e.g. when the sample is executed in Interactive Window) the script won't be able to access the type. See here.

    Create & build a C# script and execute it multiple times

    var script = CSharpScript.Create<int>("X*Y", globalsType: typeof(Globals));

    script.Compile();

    for (int i = 0; i < 10; i++)

    {

    Console.WriteLine((await script.RunAsync(new Globals { X = i, Y = i })).ReturnValue);

    }

    Create a delegate to a script

    The delegate doesn't hold compilation resources (syntax trees, etc.) alive.

    var script = CSharpScript.Create<int>("X*Y", globalsType: typeof(Globals));

    ScriptRunner<int> runner = script.CreateDelegate();

    for (int i = 0; i < 10; i++)

    {

    Console.WriteLine(await runner(new Globals { X = i, Y = i }));

    }

    Run a C# snippet and inspect defined script variables

    var state = await CSharpScript.RunAsync<int>("int answer = 42;");

    foreach (var variable in state.Variables)

    Console.WriteLine($"{variable.Name} = {variable.Value} of type {variable.Type}");

    Chain code snippets to form a script

    var script = CSharpScript.

    Create<int>("int x = 1;").

    ContinueWith("int y = 2;").

    ContinueWith("x + y");

     

    Console.WriteLine((await script.RunAsync()).ReturnValue);

    Continue script execution from a previous state

    var state = await CSharpScript.RunAsync("int x = 1;");

    state = await state.ContinueWithAsync("int y = 2;");

    state = await state.ContinueWithAsync("x+y");

    Console.WriteLine(state.ReturnValue);

    Create and analyze a C# script

    using Microsoft.CodeAnalysis;

     

    var script = CSharpScript.Create<int>("3");

    Compilation compilation = script.GetCompilation();

    //do stuff

    Compilation gives access to the full set of Roslyn APIs.

    Customize assembly loading

    using Microsoft.CodeAnalysis.Scripting.Hosting;

     

    using (var loader = new InteractiveAssemblyLoader())

    {

    var script = CSharpScript.Create<int>("1", assemblyLoader: loader);

    //do stuff

    }

转载地址:http://gdtpl.baihongyu.com/

你可能感兴趣的文章
资源大集中 浪潮I9000刀片为国家税务总局打造全能型平台
查看>>
PC如何接管手机的双因子身份验证 靠的是英特尔的CPU
查看>>
分析:“AI on Hadoop”有意义吗?
查看>>
起底英特尔大数据
查看>>
《中国人工智能学会通讯》——11.65 双重代价敏感的属性分类模型
查看>>
阿里云人工智能ET夺肺结节诊断世界冠军
查看>>
研究人员发现利用Excel宏可发起跳板攻击
查看>>
绿盟科技发布OpenSSL高危漏洞技术分析与防护方案 G20成员国美国、中国、德国受影响较大...
查看>>
《VMware Virtual SAN权威指南》一2.2.4 容量层设备
查看>>
物联网发展年报显示 2016年智能家居市场快速增长
查看>>
如何在React中做到jQuery-free
查看>>
4G+宽带高歌猛进:移动双线虐杀联通
查看>>
带你了解超大规模数据中心究竟有何不同?
查看>>
用Python实现每秒处理120万次HTTP请求
查看>>
Android单元测试 - 几个重要问题
查看>>
DNS服务器不能响应的四大解决办法
查看>>
美国税局再遭攻击:原是偷来的社会安全号码作祟
查看>>
如何在Kali Linux中安装Google Chrome浏览器
查看>>
勒索软件防不胜防? 要先从了解它开始
查看>>
大数据精准医疗解读遗传密码 未来医疗健康的变革
查看>>