Question

Using Roslyn CTP3 (it has scripting support which was removed in the latest release which is why I want to use the old one) How can I get the result of a Boolean expression using the VB scripting engine? I tried the below code but I get a syntax error when it tries to compile the script code.

using Roslyn.Scripting.VisualBasic;
using System;

class Program
{
    static void Main()
    {
        var engine = new ScriptEngine();
        engine.AddReference(typeof (object).Assembly);
        engine.ImportNamespace("System");

        var session = engine.CreateSession();
        var result = session.Execute<bool>("True");
        Console.WriteLine(result);
    }
}

I tried the same thing using the CSharp ScriptEngine and it worked fine. I also tried this same code in VB (thought using VB scripting engine in VB would produce a different outcome) but I got the same error.

If you don't have that old CTP3 laying around, you can see this in these two examples using .NET Fiddle which use Roslyn CTP3...

C# http://dotnetfiddle.net/JBlNmK (works) VB http://dotnetfiddle.net/CeJAeE (syntax error)

Edit: I may have found way by wrapping a function around it... http://dotnetfiddle.net/PJJzrQ

Was it helpful?

Solution

I think this was a limitation of the implementation of the VB ScriptEngine. IIRC, the VB implementations were not as complete as the C# implementations in the CTP.

The C# implementation could execute strings either as standalone statements, code blocks, or single expressions. The VB implementation however apparently does not support single expressions, but could run standalone statements and code blocks. You won't even be able to use the generic overload of Execute<T>() for most generic types, you would need to manually cast to the appropriate type.

You would have to change your script so the value you want could be obtained through a normal VB statement. You don't have a lot of options here but a function call is a statement that evaluates to a value. That could be used.

var engine = new ScriptEngine();
var session = engine.CreateSession();
// implement your expression here
session.Execute(@"Function ExecuteExpression As Boolean
    Return True
End Function");
//var result = session.Execute<bool>("ExecuteExpression"); // error
var result = (bool)session.Execute("ExecuteExpression");

You could create an extension methods to simplify this however.

public static T ExecuteExpression<T>(this Session session, string expressionCode)
{
    return (T)session.Execute(String.Format(@"Class _SessionExtensions
    Public Shared Function ExecuteExpression As Object
        Return {0}
    End Function
End Class
_SessionExtensions.ExecuteExpression", expressionCode));
}

This will allow you to do this:

var engine = new ScriptEngine();
var session = engine.CreateSession();
var result = session.ExecuteExpression<bool>("True");
var result2 = session.ExecuteExpression<int>("123 + 4523");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top