views:

345

answers:

8
+1  Q: 

.NET scripting

Can anyone provide an example of .NET scripting?

Some dynamic languages support functions like eval() or compile() than let you compile and execute a string in run-time.

VBCodeProvider seems to be a related namespace, but i can't find an example showing how to compile and execute code.

Edit:

I'm developing a ASP.NET site using VB.NET. Some behaviour need to be dynamic and scripted

Edit:

I think that MSScriptControl is what i'm looking for ..

+1  A: 

At the moment, you'd have to use the provider to create an assembly, then load that assembly and use reflection to invoke a method.

In .NET 4.0, there is the "compiler as a service" concept, allowing pretty-much what you have just described; there are videos from PDC, essentially having an "interactive" console (typical of dynamic languages) for C#, via a REPL and the compiler service.

I believe this type of service is already available in mono.

Marc Gravell
Ack! No Reflection! Use the dynamic runtime! ;)
Travis
Sure thing - as soon as it gets released ;-p
Marc Gravell
Fair enough :-). As I'm sure you know, the reflection route is one of pain . . .
Travis
Off topic now, but as you mention, compiler as a service seems very cool. I'm very interested in having a play with that.
Travis
+4  A: 

Take a look at Powershell. This is a scripting language written with .Net. Very easy to access .Net classes, but also COM and WMI.

After seeing the edits: calling Powershell scripts from ASP.Net does not seem like a good idea, this will be too slow.

The MSScriptControl is something from the old VB6/COM days, I would not recommended here. Better to use IronPython.

GvS
Not quite what he was talking about, actually. Powershell is just a collection of small programs that you can chain together, not a dynamic language.
Will
PowerShell is a full dynamically typed scripting language. With, among others, support for script blocks (sort of like lambdas, without lexical closures), which is more than I can say for some 'programming languages'.
Tom Lokhorst
Powershell won't be right for ASP.NET. Afaik the only solution will be the LazyParser option or IronPython
Chris S
+1  A: 

F# has a REPL and can be used as a scripting language.

Brian
+5  A: 

How about Iron Python? It's has dynamic method invocation, as you describe. If you need to call this from within C#, google for 'invoke Iron Python C#'. And, despite it living on CodePlex, it will be officially supported in the next release.

Also, .Net has support for JavaScript, I believe, which is also dynamic.

Travis
A: 

You can use Reflection.Emit to define and execute run-time dynamic methods.

[Edit:] Oops, sorry.. Not very useful if you want to generate code from a string. A CodeDomProvider is the one to use here. Here is an example (haven't tested it though).

Groo
Powerful, yes; but not exactly compatible with "compile and execute a string"...
Marc Gravell
+1  A: 

Two I have used and would recommend trying:

NB these are C# not VB.NET so I'm not sure it fully answers the question

Lazy Parser

Lazy Parser project, performs dynamic compilation.

The syntax is C#, example:

ParserContext context = new ParserContext();

context.AddType("Math", typeof(Math));
context.Set("SomeString", "Hi there!");
context.Set("SomeNumber", 20);
context.AddFunction("fmt",  typeof(String), "Format");

CSharpParser parser = new CSharpParser();

string stringValue = parser.Evaluate<string>("fmt(\"I said: {0}\", SomeString)", context);  // returns "I said: Hi there!"

Flee

Fast Lightweight Expression Evaluator

Example:

// Define the context of our expression
ExpressionContext context = new ExpressionContext();
// Allow the expression to use all static public methods of System.Math
context.Imports.AddType(typeof(Math));

// Define an int variable
context.Variables["a"] = 100;

// Create a dynamic expression that evaluates to an Object
IDynamicExpression eDynamic = context.CompileDynamic("sqrt(a) + pi");
// Create a generic expression that evaluates to a double
IGenericExpression<double> eGeneric = context.CompileGeneric<double>("sqrt(a) + pi");

// Evaluate the expressions
double result = (double)eDynamic.Evaluate();
result = eGeneric.Evaluate();

// Update the value of our variable
context.Variables["a"] = 144;
// Evaluate again to get the updated result
result = eGeneric.Evaluate();

Also worth a try:

LUA for .NET

Chris S
+1  A: 

Take a look at the DLR (Dynamic language runtime) you can easilly embed an interpreter for dynamic languages that run on it in your code. Here's an example on how to do this for IronPython: http://www.voidspace.org.uk/ironpython/hosting_api.shtml but I've seen this work for Powershell too.

Mendelt
A: 

You may as well add boo to your list. It's python-y, but not IronPython. It also plays nicely with SharpDevelop.

UPDATE: Powershell is, well, very powerful, but I confess to being rather old-school when it comes to shells, and find it alarming when I can open gui windows with shell commands. The language is also much clunkier than your usual scripting language. Its only advantage--and it's a very big one--is that, for nuts-and-bolts Windows administration, it may be the best tool out there.

Adam
it was modeled on bash from the unix world, which i always hated.
Matt Briggs