// Engine implementation:
Using system;
Using system. collections;
Using system. IO;
Using system. reflection;
Using Microsoft. VisualBasic. VSA;
Using Microsoft. VSA;
// Note: You need to add references to Microsoft. VSA. dll, Microsoft. VisualBasic. VSA. dll, and Microsoft. jscript. dll.
Namespace toolpackages
{
Public class scriptcontrol
{
Static scriptcontrol ()
{
}
Public void close ()
{
Engine. Close ();
}
Private ivsaengine engine;
Private string rootnamespace;
Private string rootmoniker;
Private scriptcontrolsite site;
Public ivsaerror Error
{
Get {return site. error ;}
}
Public void run ()
{
If (! Engine. isrunning)
{
If (! Engine. iscompiled)
Engine. Compile ();
If (engine. iscompiled = false)
{
String message = "error on line" + error. line + ", col" + error. endcolumn + "/R/N" + error. linetext + "/R" + error. description;
Throw new exception (Message );
}
Engine. Run ();
}
}
Public void addcode (string itemname, string code)
{
Ivsacodeitem item = getcodeitem (itemname );
Item. appendsourcetext (CODE );
}
Public scriptcontrol (string rootnamespace): This (rootnamespace, "sharpnessdotnetapp: // sharpnessdotnetproject", "Microsoft. VisualBasic. VSA. vsaengine ")
{
}
Public scriptcontrol (string rootnamespace, string rootmoniker, string LANGUAGE)
{
This. rootnamespace = rootnamespace;
This. rootmoniker = rootmoniker;
Switch (language. tolower ())
{
Case "Microsoft. visualbaisc. VSA ":
Engine = new vsaengine ();
Break;
Case "Microsoft. JScript. VSA ":
Engine = new Microsoft. JScript. VSA. vsaengine ();
Break;
Default:
Engine = new vsaengine ();
Break;
}
Site = new scriptcontrolsite ();
Engine. rootmoniker = rootmoniker;
Engine. Site = site;
Engine. initnew ();
Engine. rootnamespace = rootnamespace;
}
Public void addreference (string fullname)
{
Addreference (assembly. Load (fullname ));
}
Public void addreference (type T)
{
Addreference (T. Assembly );
}
Public void addreference (Assembly ASM)
{
Try
{
Ivsareferenceitem item = (ivsareferenceitem) engine. Items. createitem (ASM. Location, vsaitemtype. Reference, vsaitemflag. None );
Item. assemblyname = ASM. location;
}
Catch (Microsoft. VSA. vsaexception exception)
{
String message = Exception. message;
}
}
Public void addobject (string name, object OBJ)
{
Site. addobject (name, OBJ );
Type T = obj. GetType ();
If (! (Engine is vsaloader ))
{
Ivsaglobalitem globalitem;
Globalitem = (ivsaglobalitem) engine. Items. createitem (name, vsaitemtype. appglobal, vsaitemflag. None );
Globalitem. typestring = T. fullname;
}
}
Public void addeventsourceobject (string itemname, string name, object OBJ)
{
If (! (Engine is vsaloader ))
{
Ivsacodeitem codeitem;
Codeitem = getcodeitem (itemname );
If (codeitem. sourcetext = NULL | codeitem. sourcetext. Length = 0)
Throw new invalidoperationexception ("No scripts to run ");
Codeitem. addeventsource (name, obj. GetType (). fullname );
}
Site. addeventsourceobject (itemname, name, OBJ );
}
Public object invoke (string methodname, object [] arguments)
{
If (null = methodname) | (0 = string. Compare (methodname ,"")))
Throw new argumentnullexception ("methodname ");
If (false = engine. isrunning)
Run ();
Char [] seprators = {'.'};
String [] parts;
Parts = methodname. Split (seprators );
If (parts. Length <2)
Throw new argumentnullexception ("methodname ");
Int procnamepos = methodname. lastindexof ('.');
String typename = methodname. substring (0, procnamepos );
String procname = methodname. substring (procnamepos + 1 );
If (null = procname)
Throw new argumentnullexception ("methodname ");
String rootnamespace;
String fullname;
Rootnamespace = engine. rootnamespace;
Fullname = rootnamespace + "." + typename;
Type clstype = engine. Assembly. GetType (fullname, true, true );
Methodinfo method = clstype. getmethod (procname );
If (null = method)
Throw new argumentnullexception ("methodname ");
Return method. Invoke (null, arguments );
}
Private ivsacodeitem getcodeitem (string itemname)
{
If (itemname = NULL | itemname. Length = 0)
Itemname = "module1 ";
Ivsacodeitem codeitem = NULL;
Try
{
Codeitem = (ivsacodeitem) engine. items [itemname];
}
Catch
{
}
If (codeitem = NULL)
Codeitem = (ivsacodeitem) engine. Items. createitem (itemname, vsaitemtype. Code, vsaitemflag. None );
Return codeitem;
}
Private class scriptcontrolsite: ivsasite
{
Private idictionary hostobjects;
Private idictionary eventsourceobjects;
Private byte [] Pe = NULL;
Private byte [] PDB = NULL;
Public ivsaerror error;
Private byte [] Read (string filename)
{
Filestream FS = new filestream (filename, filemode. Open );
Binaryreader BR = new binaryreader (FS );
Return Br. readbytes (INT) fs. Length );
}
Public void loadfrom (string pefile, string pdbfile)
{
Pe = read (pefile );
PDB = read (pdbfile );
}
Public scriptcontrolsite ()
{
This. hostobjects = new hashtable ();
This. eventsourceobjects = new hashtable ();
}
Public void addobject (string name, object OBJ)
{
This. hostobjects. Add (name, OBJ );
}
Public void addeventsourceobject (string itemname, string name, object OBJ)
{
Idictionary eventsource;
If (! Eventsourceobjects. Contains (itemname ))
{
Eventsourceobjects. Add (itemname, new hashtable ());
}
Eventsource = (idictionary) eventsourceobjects [itemname];
Eventsource. Add (name, OBJ );
}
# Region ivsasite members
Public object geteventsourceinstance (string itemname, string eventsourcename)
{
Idictionary eventsoruce = (idictionary) eventsourceobjects [itemname];
Return eventsoruce [eventsourcename];
}
Public object getglobalinstance (string name)
{
Return hostobjects [name];
}
Public void Policy (string policy, object info)
{
}
Public bool oncompilererror (ivsaerror error)
{
Error = error;
Return false;
}
Public void getcompiledstate (Out byte [] PE, out byte [] debuginfo)
{
Pe = This. pe;
Debuginfo = This. PDB;
}
# Endregion
}
}
}
// Call the demo part:
Using system;
Using system. Threading;
Using system. Text. regularexpressions;
Using system. reflection;
Using system. collections;
Using system. text;
// Note: You need to add references to Microsoft. VSA. dll, Microsoft. VisualBasic. VSA. dll, and Microsoft. jscript. dll.
Namespace toolpackages
{
Public class sayhelloclass
{
Public void test ()
{
Console. writeline ("Hello world .");
}
}
Public class vasshareexample
{
Protected static string jstemplate = @"
Import system;
Import system. collections;
{Imports}
Public class module1 {
Static function execute ()
{
{0}
}
}
";
Static void vastest (string stargetjscript, string importnamespaces)
{
// Global object definition
Idictionary objects = new hashtable ();
Objects. Add ("globalvars", "Global var test in VSA ");
String Language = "Microsoft. JScript. VSA ";
Scriptcontrol control = new scriptcontrol ("sharpnessdotnet. VSA", "MyApp: // myproject/" + guid. newguid (). tostring (), language );
/// Add reference to the Assembly
Assembly assem = assembly. getexecutingassembly ();
Control. addreference (assem );
// Add System
Control. addreference (typeof (console ));
// Add other types to be referenced in the script
// Control. addreference (typeof (anytype ));
// Add a Global Object
Foreach (string key in objects. Keys)
{
If (objects [Key]! = NULL)
Control. addobject (Key, objects [Key]);
}
Stringbuilder sb = new stringbuilder ();
// Replace the template code
SB. append (jstemplate. Replace ("{0}", stargetjscript). Replace ("{imports}", importnamespaces ));
Control. addcode ("module1", SB. tostring ());
Try
{
Control. Run ();
}
Catch (exception E)
{
Throw new exception ("cocould not load scripting engine for VSA language:" + language, e );
}
Try
{
Control. Invoke ("module1.execute", new object [] {});
}
Catch (exception E)
{
Throw new exception ("VSA script/function cocould not run", e );
}
Finally
{
Control. Close ();
}
}
Static void main ()
{
String targetscript = @"
VaR message = 'run in script with VSA ';
Try
{
Console. writeline ('successfully enters the VSA application world ...');
Console. writeline ('the following output is a call to a public static method of a public class :');
Vasshareexample. Write (Message );
VaR A = 1;
VaR B = 2;
VaR c = 1 + 2;
Console. writeline ('the following output is the value of the global variable [globalvars :');
Console. writeline (globalvars );
Console. writeline ('the following output is the value of the simple calculation [1 + 2 :');
Console. writeline (C );
Console. writeline ('the following output is a call to a public instance method of a public class :');
VaR sayhello = new sayhelloclass ();
Sayhello. Test ();
Console. writeline ('Enter the string :');
VaR input = console. Readline ();
Console. writeline ('the string you entered is :');
Console. writeline (input );
Console. writeline ('start application jump :');
Vasshareexample. skip1 ();
}
Catch (E)
{
Console. writeline (E );
}
";
String targetnamespace = "Import toolpackages;/R/N ";
Vastest (targetscript, targetnamespace );
Console. writeline ("returned from the VSA call to the Host application. Goodbye ~ ");
Console. Readline ();
}
Static void checkformat ()
{
Bool result = test ("", @ "[^/S] {1,} $ ");
Console. writeline (result. tostring ());
}
Public static void skip1 ()
{
Console. writeline ("The skip1 method has been successfully jumped to. This method will jump to the skip2 method now ");
Skip2 ();
}
Public static void skip2 ()
{
Console. writeline ("The skip2 method has been successfully jumped to. This method will jump to the skip3 method now ");
Skip3 ();
}
Public static void skip3 ()
{
Console. writeline ("successfully redirected to the skip3 method, this method attempts to force the application to exit ");
Console. writeline (thread. currentthread. threadstate. tostring ());
Thread. currentthread. Interrupt ();
}
Public static void write (string message)
{
Console. writeline (Message );
}
Static bool test (string curvalue, string format)
{
Return RegEx. Match (curvalue, Format). success;
}
}
}