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

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

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
    public class Class
        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=, Culture=neutral, 

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

More information about the Ironpython-users mailing list