【问题标题】:Change FilePath of CS-Script AsmHelper Object更改 CS-Script AsmHelper 对象的 FilePath
【发布时间】:2017-07-05 22:25:40
【问题描述】:

我正在使用CS-Script 库中的AsmHelper 类。我想知道如何只使用一个 AsmHelper 对象并使用CSScript.LoadCode 更改文件路径,这样我就可以在前一个脚本完成后加载一个新脚本,但只有一个 AsmHelper 对象。现在,如下图,我有四个 AsmHelper 对象。

        AsmHelper transform1 = new AsmHelper
            (CSScript.LoadCode(@"CS-Scripts\parent.cs", null, true));
        ITransform engine1 = (ITransform)transform1.CreateObject("Script");

        AsmHelper transform2 = new AsmHelper
            (CSScript.LoadCode(@"CS-Scripts\bYear.cs", null, true));
        ITransform engine2 = (ITransform)transform2.CreateObject("Script");

        AsmHelper transform3 = new AsmHelper
            (CSScript.LoadCode(@"CS-Scripts\fInitial.cs", null, true));
        ITransform engine3 = (ITransform)transform3.CreateObject("Script");

        AsmHelper transform4 = new AsmHelper
            (CSScript.LoadCode(@"CS-Scripts\pet.cs", null, true));
        ITransform engine4 = (ITransform)transform4.CreateObject("Script");

如果需要,这里是它的界面:

public interface ITransform
{
    User Transform(User record);
}

如果您需要我提供更多详细信息,请在下方提问。

【问题讨论】:

    标签: c# cs-script


    【解决方案1】:

    您不应重复使用AsmHelper。它是为单个脚本用例设计的早期。这就是它是 IDisposable 的原因。

    AsmHelper 的职责是通过“Dispatch”调用模型访问脚本方法,在远程 AppDomain 中加载脚本程序集时处理特定于脚本的程序集探测和卸载脚本程序集。

    由于您没有使用这些功能,您最好使用更新的 API:

    var engine1 = (ITransform)CSScript.LoadCode(@"CS-Scripts\parent.cs", null, true)
                                      .CreateObject("Script");
    
    var engine2 = (ITransform)CSScript.LoadCode(@"CS-Scripts\bYear.cs", null, true)
                                      .CreateObject("Script");
    
    var engine3 = (ITransform)CSScript.LoadCode(@"CS-Scripts\fInitial.cs", null, true)
                                      .CreateObject("Script");
    
    var engine4 = (ITransform)CSScript.LoadCode(@"CS-Scripts\pet.cs", null, true)
                                      .CreateObject("Script");
    

    如果您安装 CS-Script NuGet 包,它将向您的 VS 项目添加一些演示这些技术的示例文件。

    【讨论】:

    • 我以前是这样做的,但我使用 AsmHelper 的原因是我可以卸载它并节省内存。
    • 你知道如何卸载 AsmHelper 吗?
    • 但是您在原始帖子中提供的代码永远不会卸载。您应该使用不同的 AsmHelper 构造函数来确保 AppDomain 在处理时卸载。查看代码示例,您将了解它是如何完成的。
    猜你喜欢
    • 2022-01-08
    • 2023-03-12
    • 2020-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-28
    • 2012-01-02
    相关资源
    最近更新 更多