-
Notifications
You must be signed in to change notification settings - Fork 37
020 RuntimeHost
Runtime Host is core integration point for scripting run-time infrastructure.
It should be initialized before using any other class from script.net library.
There few ways to initialize runtime host. The simplest is to call:
RuntimeHost.Initialize();
This will initialize runtime with a default configuration. Note that default configuration for Win32 version is different to Silverlight and CF versions.
There is this overloaded version of Initialize method which allows to use custom configuration:
public static void Initialize(Stream configuration);
Where stream should represent valid Xml configuration file stream. Among configuration options there are few other customizable parts of run time. They are:
- Binder - used to bind to object's members;
- Activator - used to create instances of objects. Default activator uses binder to bind to type's constructor;
- Scope Factory - used to create scopes of given type;
- Assembly and Type Manager - used to load assemblies, scan them for types, manage and resolve types by given names; Default Assembly managers are different for different platforms.
These components could be customized by custom implementation and assigning the properties of RuntimeHost class:
RuntimeHost.Binder = new DefaultBinder();
RuntimeHost.Activator = new DefaultActivator();
RuntimeHost.ScopeFactory = new ScopeFactory();
RuntimeHost.AssemblyManager = new DefaultAssemblyManager();
RuntimeHost.Initialize();
Example (Using Script.NET to run the code):
using System;
using System.Collections.Generic;
using ScriptNET;
using ScriptNET.Runtime;
namespace Test
{
public partial class Program
{
public static int Main()
{
RuntimeHost.Initialize();
List<int> vals = new List<int>();
vals.AddRange(new int[] { 1, 2, 3, 4 });
Script script = Script.Compile(@"
rez = 0;
foreach (number in numbers)
rez += number;"
);
//Adding variable to script's scope
script.Context.SetItem("numbers", vals);
object rez = script.Execute();
Console.WriteLine(rez);
}
}