public void LoadCodeRemotely()
{
// Class Calc doesn't implement ICals interface. Thus the compiled object cannot be typecasted into
// the interface and Evaluator will emit duck-typed assembly instead.
// But Mono and Roslyn build file-less assemblies, meaning that they cannot be used to build
// duck-typed proxies and CodeDomEvaluator needs to be used explicitly.
// Note class Calc also inherits from MarshalByRefObject. This is required for all object that
// are passed between AppDomain: they must inherit from MarshalByRefObject or be serializable.
var script = CSScript.CodeDomEvaluator
.LoadCodeRemotely<ICalc>(
@"using System;
public class Calc : MarshalByRefObject
{
object t;
public int Sum(int a, int b)
{
t = new Test();
return a+b;
}
}
class Test
{
~Test()
{
Console.WriteLine(""Domain is unloaded: ~Test()"");
}
}
");
Console.WriteLine("{0}: {1}", nameof(LoadCodeRemotely), script.Sum(15, 3));
script.UnloadOwnerDomain();
}