3

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.

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 am looking for ..

9 Answers 9

5

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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.

4 Comments

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'.
Powershell won't be right for ASP.NET. Afaik the only solution will be the LazyParser option or IronPython
@Will - wish I could downvote comments. Your comment on Powershell not being a language, is inaccurate and stands as a source of potential confusion to newcomers.
@Cheeso I can't even remember what distinction I was trying to make. Perhaps I was drunk.
2

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.

Comments

2

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

Comments

1

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.

4 Comments

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

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

Comments

0

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.

1 Comment

it was modeled on bash from the unix world, which i always hated.
0

VB.NET with Flee

Fast Lightweight Expression Evaluator https://flee.codeplex.com

Example VB.NET:

Imports Ciloci.Flee
Imports Ciloci.Flee.CalcEngine
Imports System.Math

    Dim ec As New Ciloci.Flee.ExpressionContext
    Dim ex As IDynamicExpression
    ec.Imports.AddType(GetType(Math))

    ec.Variables("a") = 10            
    ec.Variables("b") = 40               
    ex = ec.CompileDynamic("a+b")

    Dim evalData    
    evalData = ex.Evaluate()
    Console.WriteLine(evalData)

The output : 50

Comments

0

One of the simplest libraries is SILK. This is actually a full interpreted scripting language, but it could easily be used to execute an expression.

The only thing is that it requires code to be in a main() function, but you could always wrap the expression in a function if you don't want your users to have to do that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.