![]() |
Platforms, Frameworks & Libraries »
.NET Framework »
General
Intermediate
License: The Code Project Open License (CPOL)
NScript - A script host for C#/VB.NET/JScript.NETBy Rama Krishna VavilalaNScript is a tool like WScript except that it allows for scripts to be written in .NET languages like C# and VB.NET |
C#, Windows, .NET 1.0, Dev
|
|
Advanced Search |
|
|
||||||||||||||||||
NScript is a tool similar to WScript except that it allows scripts to be written in .NET languages such as C#, VB.NET and JScript.NET. NScript automatically compiles the code into an assembly in memory and executes the assembly. The NScript setup application associates NScript with ".ncs" (for C# scripts), ".nvb" (for VB.NET scripts) and ".njs" (for JScript .NET scripts) file extensions. This enables any code written in these files to be executed directly by double clicking on these files in windows explorer. I wrote this tool when I needed to write a script for automating builds. A simple batch file was not sufficient for the task and I preferred to write code in C# as opposed to VBScript or JScript. This tool came in handy as I could modify the scripts easily and execute them by double clicking on the files in windows explorer.
using System.Windows.Forms;
class Test
{
static void Main(string[] args)
{
MessageBox.Show("Hello World!", "NScript");
}
};.ncs
You can execute the code in the file in any of the following ways:-
NScript MessageBox.ncsNScriptw MessageBox.ncsThe requirements for the code to be executed by NScript are:-
System.Web.dll
System.Web.RegularExpressions.dll
System.Web.Services.dll
System.Windows.Forms.Dll
System.XML.dll
It just has list of assembly names on each line.
The NScript solution has three projects:-
NScript and NScriptW are very much similar to each other; the former can be used to run scripts that can output to console. NScript shows error messages in console where as NSCriptW shows error messages using message boxes. It is NScriptW which is associated with file extensions. Since there is lot of code that is shared by the two executables the common code is compiled in NScriptLib and both the executables refer to this class library.
The code behind NScript is pretty simple :-
CompileAndExecuteRoutine asyncDelegate = new
CompileAndExecuteRoutine(this.CompileAndExecute);
IAsyncResult result = asyncDelegate.BeginInvoke(fileName,
newargs, this, null, null);
//For a windows app a message loop and for a
// console app a simple wait
ExecutionLoop(result);
asyncDelegate.EndInvoke(result);
AppDomain where it does the main compilation and execution. This is
done because the user may require to cancel the execution of he script, in
that case the AppDomain can simply be unloaded.//Create an AppDomain to compile and execute the code
//This enables us to cancel the execution if needed
executionDomain = AppDomain.CreateDomain("ExecutionDomain");
IScriptManager manager = (IScriptManager)
executionDomain.CreateInstanceFromAndUnwrap(
typeof(BaseApp).Assembly.Location,
typeof(ScriptManager).FullName);
manager.CompileAndExecuteFile(file, args, this);
IScriptManager interface is implemented by the type ScriptManager. Since any object has to be marshaled in order for it to be referenced
from a separate AppDomain, we need the interface IScriptManager (as opposed to directly using the
ScriptManager object). The ScriptManager
type extends the MarshalByRefObject as it is marshaled by reference.
public class ScriptManager : MarshalByRefObject, IScriptManager
ScriptManager object's
CompileAndExecute method. It used CodeDOM to
carry out the compilation. It first figures out the CodeDomProvider to use based on the extension of the input script file
//Currently only csharp scripting is supported
CodeDomProvider provider;
string extension = Path.GetExtension(file);
switch(extension)
{
case ".cs":
case ".ncs":
provider = new Microsoft.CSharp.CSharpCodeProvider();
break;
case ".vb":
case ".nvb":
provider = new Microsoft.VisualBasic.VBCodeProvider();
break;
case ".njs":
case ".js":
provider = (CodeDomProvider)Activator.CreateInstance(
"Microsoft.JScript",
"Microsoft.JScript.JScriptCodeProvider").Unwrap();
break;
default:
throw new UnsupportedLanguageExecption(extension);
}
CodeDomProvider we can compile the file into a temporary assembly using the
ICodeCompiler interface
System.CodeDom.Compiler.ICodeCompiler compiler =
provider.CreateCompiler();
System.CodeDom.Compiler.CompilerParameters compilerparams =
new System.CodeDom.Compiler.CompilerParameters();
compilerparams.GenerateInMemory = true;
compilerparams.GenerateExecutable = true;
System.CodeDom.Compiler.CompilerResults results =
compiler.CompileAssemblyFromFile(compilerparams, file);
results.CompiledAssembly.EntryPoint.Invoke(
null, BindingFlags.Static, null, new object[]{args}, null);
This is a very simple tool and I must confess that this is in no way an original idea. Don Box wrote a similar tool long time back but I could not locate it. As a result I decided to write my own. In future I hope to enhance it by allowing to compile and execute XML files similar to ".wsh" files. As usual any suggestions are welcome.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 18 Nov 2002 Editor: James T. Johnson |
Copyright 2002 by Rama Krishna Vavilala Everything else Copyright © CodeProject, 1999-2009 Web11 | Advertise on the Code Project |