tags:

views:

111

answers:

2

Is it possible to get a reference to a Silverlight method purely by name from Javascript, and then invoke it? With pure Javascript objects you would be something like this:

var f = theObj["theMethodName"];    
f.call(theObj, "an arg");

But treating a Silverlight object as an associative array doesn't seem work.

I'm guessing I could probably use Eval as a last resort, but I'd rather avoid it.

+1  A: 

HtmlPage.Window.Invoke("theMethodName", "An arg");

OR

var obj = HtmlPage.Document.GetElementByID("theObj"); obj.Invoke("theMethodName", "an Arg");

...

Ah, re-reading it...no, no access to the reflection API. You'd have to expose it formally. Its still a managed object...just exposed as an 'object' in JScript. So not the same as a prototype object.

Shawn Wildermuth
These are methods of invoking Javascript methods from Silverlight, right? Kevin's looking to invoke Silverlight methods from Javascript.
Jon Galloway
AFAICT that's for calling Javascript from Silverlight. I'm going the other way - calling Silverlight from javascript
Kevin Dente
+1  A: 

The question is on how to call a Silverlight function from Javascript by name. You can easily call methods on an object directly by enabling a method for scripting using the ScriptableMember attribute, but you can't invoke it as a string directly.

I think you're stuck with eval.

Jon Galloway