This project is read-only.

Passing CLR Objects

Nov 6, 2009 at 11:01 PM

Hi Leppie,

Could you provide an example of passing CLR objects between C# and Scheme?  I'm trying to call a method of an object created in C# from IronScheme and it appears I need to pass that object into the scripting environment.  This just seems to be one heck of a problem.


Nov 7, 2009 at 6:58 AM

Hi Nathan

To do this, the easiest is to use the extension methods.

"(scheme-proc {0})".Eval(someobject);
Else you can get a reference to 'scheme-proc' and call it directly.
var proc = "scheme-proc".Eval<Callable>();
var result = proc.Call(someobject);

Then within scheme-proc you just call the method/property/etc.

(define (scheme-proc foo)
  (clr-call Foo Bar foo 1 2 3))
Cheers leppie
Nov 7, 2009 at 8:51 AM

If scheme-proc is in some library, you will need to import that library.

(library (mylib)
  (export scheme-proc)
  (import (rnrs)
          (ironscheme clr))

  (define (scheme-proc foo)
    (clr-call Foo Bar foo 1 2 3)))

You will have to call "(import (mylib))".Eval() first, then you can call "scheme-proc".Eval().

Nov 8, 2009 at 4:51 AM

Ah, good!  That means libraries can be imported with the Eval method.  When I try to use Eval on an import procedure, my code breaks.  For example, when I try:
  "(display \"Hello, World!\")".Eval();
   "(import (ironscheme clr))".Eval();
 I get:
  Hello, World!
  Unhandled exception:
  &message: "no expression in body"
    form: ()
    subform: #f

If it's possible to use Eval, it will make writing my code so much easier.  I was just worried that Eval couldn't use the import procedure.

Nov 8, 2009 at 6:42 AM

Hmmm, that looks like a bug, let me check.

Nov 8, 2009 at 6:46 AM


It is a bug, thanks for finding it  :)

Will have to rethink the process, as it is a bit more serious than it appears to be  :(



Nov 8, 2009 at 7:04 AM

Partially fixed in the latest check-in, but should be ok for most cases.

The current limitation is that you cannot eval definitions or imports that have any parameters.

Example: "(define x {0})".Eval(1) wont work.