[IronPython] Marking Methods as Scriptable from IronPython with Silverlight (and problems therein)

Michael Foord fuzzyman at voidspace.org.uk
Fri Jul 20 14:33:01 CEST 2007


John Messerly wrote:
> What happens if you change method() to return a string, e.g.:
>
> [Scriptable]
> public string method()
>
> Does that help?
>   

Uhm... :-o  yes it does.

Sorry. I spent so long banging head against the C# example (there my 
Javascript was trying to call into C# before the Silverlight control had 
properly loaded - so I did learn something useful), that I missed that 
kind of obvious mistake. :-)

Thanks for your patience.

Michael

> -----Original Message-----
> From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Michael Foord
> Sent: Thursday, July 19, 2007 4:33 PM
> To: Discussion of IronPython
> Subject: [IronPython] Marking Methods as Scriptable from IronPython with Silverlight (and problems therein)
>
> Hello all (especially John),
>
>
> I've been experimenting with calling into IronPython from Javascript
> using Silverlight.
>
> The way to call into managed code is to mark classes and methods with a
> Scriptable attribute. Obviously you can't do this from IronPython -
> still no attributes support ;-). The obvious way to attempt is to create
> a C# class with the attributes and subclass from IronPython.
>
> I have a basic C# example working, so I know I'm on the right track.
> Unfortunately it fails to work from IronPython (either using the C#
> class directly or a subclass). The error is:
>
> Script object of type 'ScriptableClass.Class' contains a script member
> 'method' with an invalid type ('System.Object').
> Exception Details: System.NotSupportedException: Script object of type
> 'ScriptableClass.Class' contains a script member 'method' with an
> invalid type ('System.Object')..
>
> Stack Trace:
>
>   at OnLoad in test.py, line 22
>
>
> This could be because it is only valid to return a string or other
> primitive type from a Scriptable method. I wonder if when done from
> inside IronPython the method signature is mutated to appear as if it
> returns Object ?
>
>
> Here is the basic C# code:
>
> using System;
> using System.Windows.Browser;
>
> namespace ScriptableClass
> {
>     [Scriptable]
>     public class Class
>     {
>         [Scriptable]
>         public object method()
>         {
>             return this._method();
>         }
>
>         public object _method()
>         {
>             return "hello";
>         }
>     }
>
> }
>
>
> The Python code that calls it (adding the reference is working now - God
> knows why - must have been an oddity on my system - sorry):
>
> import clr
> clr.AddReference("ScriptableObjects, Version=1.0.0.0, Culture=neutral,
> PublicKeyToken=null")
>
> import System
> import System.Windows.Browser
>
> from System.Windows import WebApplication
> from System.Windows.Browser import HtmlPage
>
>
> from ScriptableClass import Class
>
> class Subclass(Class):
>     def _method(self):
>         return 'wobble'
>
>
> def OnLoad(sender, e):
>     example1 = Class()
>     example2 = Subclass()
>
>     WebApplication.Current.RegisterScriptableObject("test1", example1)
>     WebApplication.Current.RegisterScriptableObject("test2", example2)
>
>
> The error is at the *first* call to "RegisterScriptableObject" - so it
> isn't even with the subclass...
>
> If anyone has any clues as to how I might work round this then it would
> be much appreciated. (I have already tried moving the call to
> "RegisterScriptableObject" into C#, but still passing in instances
> created in Python code and this made no difference.)
>
> All the best,
>
>
> Michael Foord
> http://www.voidspace.org.uk/ironpython/index.shtml
>
> _______________________________________________
> users mailing list
> users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
> _______________________________________________
> users mailing list
> users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>
>   




More information about the Ironpython-users mailing list