[IronPython] importing C# functions

Slide slide.o.mix at gmail.com
Wed Aug 9 02:56:56 CEST 2006


Ah, so the IP runtime will automatically invoke delegates. That is
exactly what I am looking for. Thanks!



On 8/8/06, Ryan Dawson <rdawson at exchange.microsoft.com> wrote:
> It may be I'm misunderstanding the question, but I believe you're asking for something like this:
>
>     using System;
>     using IronPython.Hosting;
>
>     class Test{
>         public static void Main(){
>             Test t = new Test();
>             PythonEngine pe = new PythonEngine();
>
>             Del d = new Del(t.AddIt);
>             pe.Globals["AddIt"] = d;
>
>             int result = pe.EvaluateAs<int>("AddIt(3,2)");
>             Console.WriteLine("Result = {0}",result);
>         }
>
>         delegate int Del(int a, int b);
>         public int AddIt(int a, int b){
>             return a+b;
>         }
>     }
>
> This is, at runtime, adding the method AddIt into the default module for the hosted engine and making it callable via IronPython code.  This is essentially the same as your suggested "AddMethod" API.
>
> The method added certainly does not have to be static.  Further, I added it to the default module, but you have similar access to the globals of any module.
>
> Hope that helps,
>         -Ryan
>
>
>
> -----Original Message-----
> From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Slide
> Sent: Saturday, August 05, 2006 11:50 AM
> To: Discussion of IronPython
> Subject: Re: [IronPython] importing C# functions
>
> So I just need to make sure my functions are static? The main goal of this was to allow runtime addition of functions for a module. I was hoping to do something like
>
> EngineModule mod = _engine.CreateModule("mymodule", true);
>
> mod.AddMethod(someObject, "mymethod", someDelegate);
>
> Then I would be able to call mymodule.mymethod inside of python scripts, but I guess that might be a pipe dream.
>
> thanks
>
> slide
>
>
> On 8/5/06, Stefan Rusek <s at rusek.org> wrote:
> > Tell him to look into using IronPython instead of normal python. IP is
> > written completely in C# and has really good .net support, and he
> > should be able to declare a static method and use it like normal function in python.
> >
> > --Stefan
> >
> > -----Original Message-----
> > From: users-bounces at lists.ironpython.com
> > [mailto:users-bounces at lists.ironpython.com] On Behalf Of Michael Foord
> > Sent: Saturday, August 05, 2006 7:37 AM
> > To: users at lists.ironpython.com
> > Subject: Re: [IronPython] importing C# functions
> >
> >
> > Slide wrote:
> > > I am currently adding a scripting console to one of the applications
> > > I develop. I would like to, at runtime, add functions that can be
> > > called by the python code in the console. Something like
> > >
> > > // c#
> > > public int AddIt(int a, int b)
> > > {
> > >     return a + b;
> > > }
> > >
> >
> > Does C# allow you to define functions outside of objects ? I didn't
> > think so...
> >
> > You can create static methods on objects though.
> >
> > > then, I would like to add that function (and possibly functions from
> > > plugins) so they can be called from python. Do I need to create a
> > > module and add the functions to the module? Is there a better way?
> > >
> >
> > For an example of creating a dll from C#, and then accessing from
> > IronPython, see :
> >
> > http://www.voidspace.org.uk/python/weblog/arch_d7_2006_07_29.shtml#e40
> > 6
> >
> > All the best,
> >
> > Fuzzyman
> > http://www.voidspace.org.uk/python/index.shtml
> >
> > > Thanks,
> > >
> > > slide
> > > _______________________________________________
> > > 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
> >
> > _______________________________________________
> > 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
> _______________________________________________
> 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