public static void ExecuteAndUnload()
{
// The script will be loaded into a temporary AppDomain and unloaded after the execution.
// Note: remote execution is a subject of some restrictions associated with the nature of the
// CLR cross-AppDomain interaction model:
// * the script class must be serializable or derived from MarshalByRefObject.
//
// * any object (call arguments, return objects) that crosses ApPDomain boundaries
// must be serializable or derived from MarshalByRefObject.
//
// * long living script class instances may get disposed in remote domain even if they are
// being referenced in the current AppDomain. You need to use the usual .NET techniques
// to prevent that. See LifetimeManagement.cs sample for details.
var code = @"using System;
public class Script : MarshalByRefObject
{
public void Hello(string greeting)
{
Console.WriteLine(greeting);
}
}";
//Note: usage of helper.CreateAndAlignToInterface<IScript>("Script") is also acceptable
using (var helper = new AsmHelper(CSScript.CompileCode(code), null, deleteOnExit: true))
{
IScript script = helper.CreateAndAlignToInterface<IScript>("*");
script.Hello("Hi there...");
}
//from this point AsmHelper is disposed and the temp AppDomain is unloaded
}