_ Toc245285206 "> level one -- script runtimes, scopes,
And executing files and snippets
For simple application programmability, you
Want to provide a host object model that dynamic versions code can use. You then want to execute files of code that
Consume that object model. You may also
Want to get the values of variables from the dynamic language code to use
Dynamic functions as command implementations or event handlers.
There are two types you will use at this
Level. The scriptruntime class is
Starting point for hosting. You create
Runtime with this class.
Scriptruntime represents global script state, such as referenced assemblies and
A Global Object (A scriptscope).
Scriptscope class essential tially represents a namespace. Hosts can bind variable names in scriptscopes,
Fetch variable values, etc. Hosts can
Execute Code within different scopes to isolate free variable resolutions.
There are a lot of Members on these types
Because they are also used for level two and Level Three. For level one you only need a few members and
Can ignore the rest. You need to create
A scriptruntime, from which you might use executefile or globals. The globals object lets you set variables
Provide access to a host object model.
From scriptscope, you will likely only use getvariable and setvariable.
Code sample -- Application
Programmability
The following code sample assumes you have
A default app. config file (see Section 4.13.3.2 ):
Code Public class level_1 {
Scriptruntime Env = scriptruntime. createfromconfiguration ();
Myhostobjectmodel hostom = new myhostobjectmodel ();
/// <Summary>
/// Shows setting host om on globals so that dynamic ages
/// Can import, require, Etc., to access the host's Om.
/// </Summary>
/// My_user_script.py:
/// Import hostmodule
/// Def Foo ()...
/// Hostmodule. usercommands ["foo"] = foo
///
Public void runfile_isolated_scope_importsfromhost (){
Env. globals. setvariable ("hostmodule", hostom );
// Imagine this runs my_user_script.py above.
Env. executefile (getfilefromuserorsettings ());
}
Delegate void command ();
/// <Summary>
/// Shows getting command implementations from Dynamic Language.
/// Assumes menu item text is command name in table.
/// Builds on previous function.
/// </Summary>
Public void run_user_command_from_menuitem (string menuitemname ){
// Usercommands is dictionary <string, command>.
Hostom. usercommands [menuitemname] ();
}
/// <Summary>
/// Shows discovering command implementations from globals in scope.
/// Above User Code explicitly added commands, but this code finds
/// Commands matching a delegate type, command.
/// </Summary>
Public void collect_user_commands_from_file (string menuitemname ){
Env. globals. setvariable ("hostmodule", hostom );
Scriptscope Scope
= Env. executefile (getfilefromuserorsettings ());
// Usercommands is dictionary from string to command.
Command fun;
Foreach (string ID in scope. getvariablenames ()){
Bool got_fun = scope. trygetvariable <command> (ID, out fun );
If (got_fun ){
My_om.usercommands [ID] = fun;
}
}
}
}