I have a WebBrowser control. I have added some JavaScript into the head tag and I can see it is working as expected by adding an alert. Inside of this js I am creating a function and adding some members to it's prototype like so:
function test() {
}
test.prototype.run = function() {
alert('success!')
}
function createTest() {
return new test()
}
Then back inside of C# I am doing:
dynamic test = this.browser.InvokeScript("createTest");
test.run();
I can see that the test object is some ComObject but when I call run() nothing happens. I get no error but nothing happens. Does anyone know how to call this type of custom object?
Also suppose I wanted to get rid of the createTest() method, how can I create a new instance of test from C#?
Also, for bonus points, is there anything special I need to know about attaching events to this custom object (on say a 'complete' member) such that it will callback into my C# code?
Turns out that the "dynamic" keyword isn't smart enough to figure this out for some reason. What you can do is cast the COM object returned from InvokeScript into IExpando (or IReflect) and call the InvokeMember method. I created a class that inherits from DynamicObject that overrides all TryXYZ methods and transforms them into IReflect/IExpando method calls. Then you can interact with the object like the above snippet.