Call known method by name

Mar 5, 2015 at 4:06 PM
Edited Mar 5, 2015 at 4:07 PM
Per this thread here:

I should be able to do
ScriptEngine.Script["MyMethod"](arg1, arg2);
But I get exception:
Cannot apply indexing with [] to an expression of type 'System.Dynamic.DynamicObject'
Per this thread here:

the extension method blows up in the same way.
var del = (Delegate)host.func<object>(args.Length, engine.Script[name]);
engine.Script[name] is not allowed.

Am i missing something basic??
Mar 5, 2015 at 4:58 PM

The following test code compiles and runs as expected:
using (var engine = new V8ScriptEngine())
    engine.Execute("function foo(x) { return x; }");
    Console.WriteLine(;  // writes "123"
    Console.WriteLine(engine.Script["foo"](456));  // writes "456"
    Console.WriteLine(engine.Invoke("foo", 789));  // writes "789"

    var host = new HostFunctions();
    var del = (Delegate)host.func(1, engine.Script["foo"]);
    Console.WriteLine(del.DynamicInvoke(987));  // writes "987"
Are you doing something differently? What framework version are you using?

Mar 5, 2015 at 5:12 PM
Thanks for the quick response. I am using .Net 4.5.1

I tried your code sample in a console app, and an app, and it worked great.

Apparently my issue is that I am using a VisualWebGui application.
engine.Execute("function foo(x) { return x; }");
Console.WriteLine(;  // writes "123"
'System.Dynamic.DynamicObject' does not contain a definition for 'foo'

which mirrors the issues I am having. I can open Script in the debugger and see the foo method there.

Any ideas?
Mar 5, 2015 at 6:11 PM
Hi again,

That's very odd. It's as if your environment doesn't support C#'s dynamic keyword properly, or is missing the required runtime infrastructure.

Does Visual WebGUI use a custom C# compiler or class library?

Mar 5, 2015 at 6:53 PM
I am assuming yes since it translates c# into html elements and javascript, however I am not really able to provide with you anymore details then that. I have asked a question on the VWG forums to perhaps help clarify.
Mar 5, 2015 at 8:21 PM
OK the issue has been resolved.

In a VWG application with CLR Exception Breaking on, the error is thrown multiple times, but you can continue past them.

It also appears that creating a ScriptEngine in a VWG form constructor is bad.

Thanks for the help and sorry to bother you.
Marked as answer by julesx416 on 3/5/2015 at 1:21 PM
Mar 5, 2015 at 8:47 PM
You're absolutely right! The C# Runtime Binder throws exceptions and handles them internally. This is all by design and happens on "normal" .NET as well.