0

I am using the AsmHelper class in the CS-Script library. I am wondering how to only use one AsmHelper object and just change the filepath using CSScript.LoadCode so I can load a new script after the previous script is done, but with only one AsmHelper object. Right now, as shown below, I have four AsmHelper objects.

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

Here is the interface that goes with it if needed:

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

If you need me to provide additional details, just ask below.

Grant Meehan
  • 23
  • 2
  • 16

1 Answers1

0

You should not reuse AsmHelper. It was designed early days for a single script use-case. That's why it is IDisposable.

The AsmHelper rsponsibility is accessing script methods via 'Dispatch' invocation model, handling script specific assembly probing and unloading script assembly when it's loaded in a remote AppDomain.

Since you are not using none of these features you are much better off by using newer APIs:

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

If you install CS-Script NuGet package it will add to your VS project a few sample files that demonstrate these techniques.

taras
  • 24
  • 4
  • I was previously doing it like that but the reason I am using AsmHelper is so I can unload it and save memory. – Grant Meehan Jul 06 '17 at 16:18
  • Do you know how to unload the AsmHelper? – Grant Meehan Jul 06 '17 at 22:20
  • But the code you provided in your original post never unloads. You supposed to use different AsmHelper constructor that actually ensures AppDomain unloading upon disposal. Have a look at the code samples and you will see how it's done. – taras Jul 07 '17 at 16:36