Hosting Javascript in .NET Windows Form App? (Somewhat Advanced)

  • Thread starter Thread starter John Puopolo
  • Start date Start date
J

John Puopolo

All:

I am writing a .NET 1.1 Windows Forms app in C#. I would like to offer
users the ability to script the application via javascript.

In the "old days," I would have used the Active (ActiveX) Scripting
host (COM based) offered by Microsoft. Is this still the best way to do
it?

If this is not the way to do it, what is the new way in .NET?

In either case, where would I get a javascript engine to host?

Many thanks,
John
 
I think you still need to use the Active Scripting Host for javascript. On
the other hand, you can use the CodeDOM and support C#, J#, or Visual Basic
for your scripting instead.

Pete
 
Look into the ICodeCompiler interface. You can dynamically build and
run C# code in your application at run-time.

Here's a real quick-and-dirty sample:

void DoSomethingInAWindowsApp()
{
ICodeCompiler ic = new
CSharpCodeProvider().CreateCompiler();
CompilerResults cr = ic.CompileAssemblyFromSource(new
CompilerParameters(), @"
namespace Test
{
class DynamaCompile
{
public static int Add (int x, int y)
{
return x + y;
}
}
}");

MessageBox.Show(cr.CompiledAssembly.GetType("Test.DynamaCompile").InvokeMember("Add",
System.Reflection.BindingFlags.InvokeMethod, null, null, new
object[]{3, 4}).ToString());
}

-Alan
 
Thanks Alan, etc.

I like the idea of ICodeCompiler, etc., but what I need is something
that enables for script debugging, etc. I envision a mini-editor where
people can enter scripts and control my program (via a set of objects
the program exposes, of course). Suppose, in your example, the code
snippet contains a syntax error - or a more subtle error. Things like
this crop up all the time in development, so (unfortunately!) this
solution will not work completely for my situation - but thank you for
taking the time to put together the example.

John
 
The CompileAssemblyFromSource function takes a "CompilerParameters" option,
where you can tell it to include debugging information. Unfortunately, I am
not aware of an easy way to make use of the debugging information when
actually running the script. It is probably worth digging around, though.

The CompileAssemblyFromSource routine returns a CompilerResults that gives
you all the error messages and stuff you might need to correct syntax errors.
It is pretty well done. You can supply this information directly to your
user, or you can digest it yourself (it includes line/column numbers) and
present him/her with an error list.
 
I've just gone down this road myself. Use the ICodeCompiler stuff as
mentioned before, and look into "mdbg", a managed debugger sample w/source
provided on MSDN. I have been able to fully implement scripting in any .NET
available language (except C++ - a long story...) and have a debugger built
into my app for debugging scripts. Its a bit rough - little documentation -
but it does work. Also search for blog posts from Mike Stall - he has lots of
information on managed debugging.

Brian.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top