[IronPython] Declare a class in Python and use it in .net

Dino Viehland dinov at exchange.microsoft.com
Fri Aug 3 19:35:24 CEST 2007


The inheritance part is no problem, that will just work.  For example (warning, compiled w/ Outlook):

C#:
namespace MyNamespace {
public delegate MyClass MyDelegate();
public class MyClass {
        public virtual string Foo() {
                return "C# in the house";
        }
        public string CallFoo() {
                return Foo();
        }
        public static MyClass CreateFoo(MyDelegate creator) {
                return creator();
        }
}
}

Python:

import clr
clr.AddReference('MyAssembly')
from MyNamespace import MyClass

class PyClass(MyClass):
        def Foo(self):
                return "Python in the house"


a = PyClass()
a.Foo()                 # returns 'Python in the house'
a.CallFoo()                     # also returns 'Python in the house'

it's the creation of the Python object that's hard.  For that you need the delegate because C# won't ever see PyClass as a .NET type that it can allocate:

def createPyClass():
        return PyClass()

MyClass.CreateFoo(createPyClass)  # returns a new PyClass


-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Ori
Sent: Friday, August 03, 2007 10:28 AM
To: users at lists.ironpython.com
Subject: Re: [IronPython] Declare a class in Python and use it in .net


How can I do what I asked using delegate? I have to inherit from a class,
override a method and return an object. I guess I can manage with returning
a delegate that execute the overriden method - but it is important to me
that the metod will be a part of a class because the code has calls to other
class properties and methods.


Dino Viehland wrote:
>
> Because Python doesn't create normal .NET classes they basically have to
> be created from Python code.  You could hand a delegate (just a Python
> function that matches the delegates # of arguments) off to the C# code and
> call that from C# and get the result back.
>
> -----Original Message-----
> From: users-bounces at lists.ironpython.com
> [mailto:users-bounces at lists.ironpython.com] On Behalf Of Ori
> Sent: Thursday, August 02, 2007 10:53 PM
> To: users at lists.ironpython.com
> Subject: [IronPython] Declare a class in Python and use it in .net
>
>
> Hello,
>
> I have a .net class (in some assembly). I want to inherit from it and
> override a function in it - with python code. I'm using c# to compile the
> python code, and after the compilation is done I want to create an
> instance
> of the new class inside c#.
>
> Is there a way to do it?
>
> Thanks,
> Ori
> --
> View this message in context:
> http://www.nabble.com/Declare-a-class-in-Python-and-use-it-in-.net-tf4210730.html#a11977831
> Sent from the IronPython mailing list archive at Nabble.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
>
>

--
View this message in context: http://www.nabble.com/Declare-a-class-in-Python-and-use-it-in-.net-tf4210730.html#a11987743
Sent from the IronPython mailing list archive at Nabble.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