Level One -- Script Runtimes, Scopes, and Executing Files and Snippets

Level One -- Script Runtimes, Scopes, and Executing Files and Snippets

For simple application programmability, you want to provide a host object model that dynamic languages 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 the starting point for hosting.  You create a runtime with this class.  The ScriptRuntime represents global script state, such as referenced assemblies and a global object (a ScriptScope).  The ScriptScope class essentially 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 to 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 ):

代码
public   class  Level_1 {
    ScriptRuntime env 
=  ScriptRuntime.CreateFromConfiguration();
    MyHostObjectModel hostOM 
=   new  MyHostObjectModel();
 
    
///   <summary>
    
///  Shows setting Host OM on globals so that dynamic languages
    
///  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;
            }
        }
    }
}

 

你可能感兴趣的:(Runtime)