• Net 编译器平台--- Roslyn Scripting APIs


    引言

    上一篇中.Net 编译器平台 --- Roslyn,介绍了Roslyn的各项功能,包括公开API,使用语法,使用语义,使用工作区等功能。

    那么回到上一篇中提到的问题,实现类似这样的功能(以下代码为伪代码):

    string scriptText = "int a = 1;int b = 2; return a+b ;";
    
    var result = Script.Run(scriptText);
    

    就用到了上一篇提到的 Scripting APIs,还是先了解一下Roslyn提供的 Scripting APIs 有哪些。

    官方文档(https://github.com/dotnet/roslyn/blob/main/docs/wiki/Scripting-API-Samples.md) 还是英文版,还是先将他翻译为中文,以下内容为译文。

    Scripting APIs Samples

    脚本 API 可以让 .NET 应用程序实例化一个 C# 引擎,并针对由宿主提供的对象执行代码片段。以下是使用脚本 API 并进行一些常见示例的入门示例。您也可以查看脚本 API 的源代码。

    请注意,作为一个语言模型,我无法提供实时的源代码示例或链接到具体的源代码。但是,您可以参考 Microsoft 的官方文档和示例来了解如何使用脚本 API 并查看相关源代码。

    支持的平台

    脚本 API 需要桌面版 .NET Framework 4.6+ 或 .NET Core 1.1(自 Roslyn v2.0.0-rc3、Visual Studio 2017 RC3 起支持)。

    脚本 API 无法在通用 Windows 应用程序和 .NET Native 中使用,因为应用程序模型不支持在运行时加载生成的代码。

    开始准备

    安装 Scripting API NuGet 包:

    Install-Package Microsoft.CodeAnalysis.CSharp.Scripting
    

    示例代码

    以下示例代码中需要添加引用 using Microsoft.CodeAnalysis.CSharp.Scripting;

    应用场景:

    • 评估一个C#表达式(Evaluate a C# expression)
    • 评估一个C#表达式(强类型)(Evaluate a C# expression(strongly-typed))
    • 带错误处理的评估C#表达式(Evaluated a C# expression with error handling)
    • 添加引用(Add references)
    • 添加命名空间和类型导入(Add namespace and type imports)
    • 为脚本参数化(Parameterize a script)
    • 创建和构建一个C#脚本,并多次执行(Create&build a C# script and execute it multiple times)
    • 创建一个指向脚本的委托(Create a delegate to a script)
    • 运行一个C#代码片段并检查定义的脚本变量(Run a C# snippet and inspect defined script variables)
    • 将代码片段链接成一个脚本(Chain code snippets to form a script)
    • 从先前状态继续执行脚本(Continue script execution from a previous state)
    • 创建和分析一个C#脚本(Create and analyze a C# script)
    • 自定义程序集加载(Customize assembly loading)

    评估一个C#表达式(Evaluate a C# expression)

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

    评估一个C#表达式(强类型)(Evaluate a C# expression(strongly-typed))

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

    带错误处理的评估C#表达式(Evaluated 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 namespace and type imports)

    在下面的代码中,WithImports("System.IO") using System.IO; 添加到脚本选项中,使得可以在脚本代码中直接引用 System.IO 命名空间的类型,而无需使用限定符。

    var result = await CSharpScript.EvaluateAsync("Directory.GetCurrentDirectory()"), 
                                    ScriptOptions.Default.WithImports("System.IO"));
    

    同样地,WithImports("System.Math")using static System.Math; 添加到脚本选项中,使得可以在脚本代码中直接引用 System.Math 类型的成员,而无需使用限定符。

    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));
    

    :::tip{title="提示"}
    目前,Globals 类型必须在从文件加载的程序集中定义。如果程序集在内存中(包括在交互式窗口中执行示例时),脚本将无法访问该类型。请参阅此处的问题。
    :::

    创建和构建一个C#脚本,并多次执行(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)

    该委托不会保持编译资源(语法树等)处于活动状态。

    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 }));
    } 
    

    运行一个C#代码片段并检查定义的脚本变量(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);
    

    创建和分析一个C#脚本(Create and analyze a C# script)

    using Microsoft.CodeAnalysis;
    
    var script = CSharpScript.Create<int>("3");
    Compilation compilation = script.GetCompilation();
    //do stuff
    

    编译(Compilation)提供了对完整的 Roslyn API 集合的访问。

    自定义程序集加载(Customize assembly loading)

    using Microsoft.CodeAnalysis.Scripting.Hosting;
    
    using (var loader = new InteractiveAssemblyLoader())
    {
        var script = CSharpScript.Create<int>("1", assemblyLoader: loader);
        //do stuff 
    }
    

    参考

    https://github.com/dotnet/roslyn/blob/main/docs/wiki/Scripting-API-Samples.md

  • 相关阅读:
    mysql oracle统计报表每天每月每年SQL
    [Vue 配置] Vite + Vue3 项目配置和使用 NProgress
    C++中多态的原理【精华】
    电商大促就靠RPA,摆脱重复劳动,急速提效
    基于python的NoneBot2+NoneBot Plugin Go-CQHTTP搭建属于自己的QQ机器人
    【吃瓜之旅】第三章吃瓜学习
    Abnova丨Abnova 抗独特型单克隆抗体的分类和特色
    解析正则表达式的底层实现原理
    intel cpu core/“酷睿”系列发展史,供组装机的朋友们参考
    云原生Kubernetes:简化K8S应用部署工具Helm
  • 原文地址:https://www.cnblogs.com/pandefu/p/17536280.html