[IronPython] FW: Returning inherited classes to .NET

Curt Hagenlocher curt at hagenlocher.org
Mon Feb 25 01:09:39 CET 2008


http://blogs.msdn.com/haibo_luo/archive/2007/09/25/5130072.aspx

On Sun, Feb 24, 2008 at 4:00 PM, Howland-Rose, Kyle <
Kyle.Howland-Rose at aar.com.au> wrote:

>  Is there a good place to read about these variants?   I was using this
> one because I did not want to use the GAC and AddReference failed just using
> "WebControlLibrary" presumably because I needed to add the dll's path to a
> path list somewhere.
>
> ------------------------------
> *From:* users-bounces at lists.ironpython.com [mailto:
> users-bounces at lists.ironpython.com] *On Behalf Of *Curt Hagenlocher
> *Sent:* Monday, 25 February 2008 10:31 AM
> *To:* Discussion of IronPython
> *Subject:* Re: [IronPython] Returning inherited classes to .NET
>
>    It's a feature.  clr.AddReferenceToFile ends up calling
> Assembly.LoadFile under the covers, which lets you load multiple versions
> of the same assembly.  If this isn't the behavior you want, you should use a
> differently variant of AddReference (such as clr.AddReference).  Each
> variant has its uses; this one's don't correspond well with your intentions.
>
> On Sun, Feb 24, 2008 at 2:22 PM, Howland-Rose, Kyle <
> Kyle.Howland-Rose at aar.com.au> wrote:
>
> >  Awesome, thanks Curt.
> >
> > Do you have any idea as to whether this is a bug or a feature?
> >
> >  ------------------------------
> > *From:* users-bounces at lists.ironpython.com [mailto:
> > users-bounces at lists.ironpython.com] *On Behalf Of *Curt Hagenlocher
> > *Sent:* Saturday, 23 February 2008 1:33 AM
> > *To:* Discussion of IronPython
> > *Subject:* Re: [IronPython] FW: FW: FW: returning inherited classes to
> > .NET
> >
> >  I built a test executable and was able to reproduce this.  And when I
> > changed
> > clr.AddReferenceToFile("WebControlLibrary.dll")
> > to
> > clr.AddReference("WebControlLibrary")
> > it worked as expected.
> > On Fri, Feb 22, 2008 at 6:02 AM, Curt Hagenlocher <curt at hagenlocher.org>
> > wrote:
> >
> > > Ah, no.  I was thinking that you would modify your VB code to launch
> > > it as a console app (for instance) rather than from inside of ASP.NET<http://asp.net/>-- and see if you still get the same error.
> > >
> > >
> > > On Thu, Feb 21, 2008 at 10:07 PM, Howland-Rose, Kyle <
> > > Kyle.Howland-Rose at aar.com.au> wrote:
> > >
> > > >  Thanks for your help so late Curt (not that I need a quick answer
> > > > at all).
> > > >
> > > > I am sure I am missing something big-time here and "talking" as
> > > > cross-purposes, but, to my mind the reason I don't have a problem when
> > > > running from the command line is because all I am doing is running the
> > > > ipy.exe on the file which loads some assemblies, creates a class
> > > > definition and a function definition and then terminates without even
> > > > instantiating the class.  Is this what you mean by "running it
> > > > directly"?
> > > >
> > > > Thanks again,
> > > > Kyle
> > > >
> > > >  ------------------------------
> > > >  *From:* users-bounces at lists.ironpython.com [mailto:
> > > > users-bounces at lists.ironpython.com] *On Behalf Of *Curt Hagenlocher
> > > > *Sent:* Friday, 22 February 2008 4:55 PM
> > > > *To:* Discussion of IronPython
> > > > *Subject:* Re: [IronPython] FW: FW: returning inherited classes to
> > > > .NET
> > > >
> > > >   If you're only getting the error under ASP.NET <http://asp.net/>,
> > > > then it's highly likely that you're loading multiple copies of the same
> > > > assembly, which is why the CLR can't cast your Python class back to the VB
> > > > base -- it literally considers them to be two different classes.  This may
> > > > be related to the way ASP.NET <http://asp.net/> makes shadow copies
> > > > of your files, but there could be other reasons as well (none of which,
> > > > unfortunately, come to mind right now).
> > > >
> > > > Using an interface instead of a base class would give you the same
> > > > results.
> > > > On Thu, Feb 21, 2008 at 9:44 PM, Howland-Rose, Kyle <
> > > > Kyle.Howland-Rose at aar.com.au> wrote:
> > > >
> > > > >  It runs fine from the command line but then it does not do much
> > > > > ... yet.  For the moment I am simplifying it by removing IP :(
> > > > >
> > > > > I wanted to so some moderately heavy reflection which to my
> > > > > thinking should be easier from IP but I will use VB for now.
> > > > >
> > > > > I assume I *should* be able to return a derived python class to
> > > > > VB?  Alternatively I could implement an interface.  How can IP implement a
> > > > > .NET interface?
> > > > > (All pointers to pre-existing doco warmly received :)
> > > > >
> > > > > Thanks
> > > > > Kyle
> > > > >
> > > > >  ------------------------------
> > > > >  *From:* users-bounces at lists.ironpython.com [mailto:
> > > > > users-bounces at lists.ironpython.com] *On Behalf Of *Curt
> > > > > Hagenlocher
> > > > > *Sent:* Friday, 22 February 2008 4:29 PM
> > > > > *To:* Discussion of IronPython
> > > > > *Subject:* Re: [IronPython] FW: returning inherited classes to
> > > > > .NET
> > > > >
> > > > >   My apologies; I wasn't reading the code correctly.  That's
> > > > > probably a sign that I should be going to bed. :)  Have you tried
> > > > > simplifying your version just a little bit by running it directly rather
> > > > > than through ASP.NET <http://asp.net/>?
> > > > >
> > > > >
> > > > > On Thu, Feb 21, 2008 at 8:43 PM, Howland-Rose, Kyle <
> > > > > Kyle.Howland-Rose at aar.com.au> wrote:
> > > > >
> > > > > > Hi Curt,
> > > > > >
> > > > > > Thanks for replying.  When I view the link I see the following:
> > > > > >
> > > > > > The .NET code creates the "Test" class and then runs the python
> > > > > > code
> > > > > > which imports the "Test" class a subclasses it to create python
> > > > > > class
> > > > > > "X".
> > > > > >
> > > > > > Class "X" is then called and cast to type "Test" and assigned to
> > > > > > the
> > > > > > value obj.
> > > > > >
> > > > > > Is this what you are looking at?
> > > > > >
> > > > > > My code is pretty much identical.  The VB looks like:
> > > > > >
> > > > > >
> > > > > > Public MustInherit Class ChildControl
> > > > > >    Public Function Control() As WebControl
> > > > > >        Return Nothing
> > > > > >    End Function
> > > > > >    Public Function GetValue() As String
> > > > > >        Return Nothing
> > > > > >    End Function
> > > > > >    Public Function SetValue(ByVal value As Object) As String
> > > > > >        Return Nothing
> > > > > >    End Function
> > > > > > End Class
> > > > > >
> > > > > > Class IPReflection
> > > > > >    Private Shared Instance As IPReflection = Nothing
> > > > > >    Private PyEngine As PythonEngine
> > > > > >    Private PyModule As EngineModule
> > > > > >
> > > > > >    Public Shared Function Inst() As IPReflection
> > > > > >        If Instance Is Nothing Then
> > > > > >            Instance = New IPReflection
> > > > > >        End If
> > > > > >        Return Instance
> > > > > >    End Function
> > > > > >
> > > > > >    Private Sub New()
> > > > > >        Dim path As String = HttpContext.Current.Request.MapPath
> > > > > > (".")
> > > > > >        PyEngine = New PythonEngine
> > > > > >        PyEngine.AddToPath(path)
> > > > > >        PyModule = PyEngine.CreateModule
> > > > > >        PyEngine.DefaultModule = PyModule
> > > > > >
> > > > > > PyEngine.ExecuteFile(HttpContext.Current.Request.MapPath
> > > > > > ("CustomControls
> > > > > > .py"))
> > > > > >    End Sub
> > > > > >
> > > > > >    Public Function GetChildControl() As ChildControl
> > > > > >        Return
> > > > > > CType(Operations.Ops.Call(PyModule.Globals("GetControl")),
> > > > > > ChildControl)
> > > > > >    End Function
> > > > > > End Class
> > > > > >
> > > > > > And the python looks like:
> > > > > >
> > > > > > import clr
> > > > > > import sys
> > > > > >
> > > > > > clr.AddReference('System')
> > > > > > import System
> > > > > >
> > > > > > sys.path.append(sys.path[0] + "/bin")
> > > > > >
> > > > > > clr.AddReferenceToFile("WebControlLibrary.dll")
> > > > > > clr.AddReference('System.Web')
> > > > > >
> > > > > > from System.Web.UI.WebControls import Label
> > > > > >
> > > > > > from WebControlLibrary import ChildControl
> > > > > >
> > > > > > class LabelChildControl(ChildControl):
> > > > > >        def __init__(self):
> > > > > >                self.control = Label()
> > > > > >        def Control(self):
> > > > > >                return self.control
> > > > > >
> > > > > > def GetControl():
> > > > > >        return LabelChildControl()
> > > > > >
> > > > > >
> > > > > > Thanks again,
> > > > > > Kyle
> > > > > >
> > > > > > ________________________________
> > > > > >
> > > > > > From: users-bounces at lists.ironpython.com
> > > > > > [mailto:users-bounces at lists.ironpython.com] On Behalf Of Curt
> > > > > > Hagenlocher
> > > > > > Sent: Friday, 22 February 2008 3:08 PM
> > > > > > To: Discussion of IronPython
> > > > > > Subject: Re: [IronPython] returning inherited classes to .NET
> > > > > >
> > > > > >
> > > > > > The page in question doesn't seem to show what you describe --
> > > > > > deriving
> > > > > > a Python class from a C# class.  Maybe you could show us your
> > > > > > sample
> > > > > > code?
> > > > > >
> > > > > >
> > > > > > On Thu, Feb 21, 2008 at 7:39 PM, Howland-Rose, Kyle
> > > > > > <Kyle.Howland-Rose at aar.com.au> wrote:
> > > > > >
> > > > > >
> > > > > >        Hi all,
> > > > > >        I am following
> > > > > >
> > > > > >
> > > > > >
> > > > > > http://www.ironpython.info/index.php/Using_Python_Classes_from_.NET
> > > > > > <
> > > > > > http://www.ironpython.info/index.php/Using_Python_Classes_from_.NET
> > > > > > >
> > > > > > but using VB.
> > > > > >
> > > > > >        In VB I declare a class and derive a python 1.1 class
> > > > > > from it
> > > > > > but when I return it I get the exception:
> > > > > >
> > > > > >        "Unable to cast object of type
> > > > > > 'IronPython.NewTypes.WebControlLibrary.ChildControl_1' to type
> > > > > > 'WebControlLibrary.ChildControl'."
> > > > > >
> > > > > >        The page referenced above just seems to cast the python
> > > > > > class
> > > > > > instance to the C# class instance but this does not work for me
> > > > > > in VB.
> > > > > >
> > > > > >        Any help would be appreciated :)
> > > > > >
> > > > > >        Thanks,
> > > > > >
> > > > > >        Kyle
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > > > ************************************************************************
> > > > > >
> > > > > >        Allens Arthur Robinson online: http://www.aar.com.au
> > > > > > <http://www.aar.com.au/>
> > > > > >
> > > > > >        This email is confidential and may be subject to legal or
> > > > > > other
> > > > > > professional privilege. It is also subject to copyright. If you
> > > > > > have
> > > > > > received it in error, confidentiality and privilege are not
> > > > > > waived and
> > > > > > you must not disclose or use the information in it. Please
> > > > > > notify the
> > > > > > sender by return email and delete it from your system. Any
> > > > > > personal
> > > > > > information in this email must be handled in accordance with the
> > > > > > Privacy
> > > > > > Act 1988 (Cth).
> > > > > >
> > > > > >
> > > > > >
> > > > > > ************************************************************************
> > > > > > *
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > > > ************************************************************************
> > > > > >
> > > > > >        Allens Arthur Robinson online: http://www.aar.com.au
> > > > > > <http://www.aar.com.au/>
> > > > > >
> > > > > >        This email is confidential and may be subject to legal or
> > > > > > other
> > > > > > professional privilege. It is also subject to copyright. If you
> > > > > > have
> > > > > > received it in error, confidentiality and privilege are not
> > > > > > waived and
> > > > > > you must not disclose or use the information in it. Please
> > > > > > notify the
> > > > > > sender by return email and delete it from your system. Any
> > > > > > personal
> > > > > > information in this email must be handled in accordance with the
> > > > > > Privacy
> > > > > > Act 1988 (Cth).
> > > > > >
> > > > > >
> > > > > >
> > > > > > ************************************************************************
> > > > > > *
> > > > > >
> > > > > >
> > > > > >        _______________________________________________
> > > > > >        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
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > > > ************************************************************************
> > > > > >
> > > > > > Allens Arthur Robinson online: http://www.aar.com.au
> > > > > >
> > > > > > This email is confidential and may be subject to legal or other
> > > > > > professional privilege. It is also subject to copyright. If you have
> > > > > > received it in error, confidentiality and privilege are not waived and you
> > > > > > must not disclose or use the information in it. Please notify the sender by
> > > > > > return email and delete it from your system. Any personal information in
> > > > > > this email must be handled in accordance with the Privacy Act 1988 (Cth).
> > > > > >
> > > > > >
> > > > > > *************************************************************************
> > > > > >
> > > > > > _______________________________________________
> > > > > > 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
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > > >
> > > > > ************************************************************************
> > > > >
> > > > > Allens Arthur Robinson online: http://www.aar.com.au
> > > > >
> > > > > This email is confidential and may be subject to legal or other
> > > > > professional privilege. It is also subject to copyright. If you have
> > > > > received it in error, confidentiality and privilege are not waived and you
> > > > > must not disclose or use the information in it. Please notify the sender by
> > > > > return email and delete it from your system. Any personal information in
> > > > > this email must be handled in accordance with the Privacy Act 1988 (Cth).
> > > > >
> > > > >
> > > > > *************************************************************************
> > > > >
> > > > > _______________________________________________
> > > > > 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
> > > > >
> > > > >
> > > >
> > > >
> > > >
> > > > ************************************************************************
> > > >
> > > > Allens Arthur Robinson online: http://www.aar.com.au
> > > >
> > > > This email is confidential and may be subject to legal or other
> > > > professional privilege. It is also subject to copyright. If you have
> > > > received it in error, confidentiality and privilege are not waived and you
> > > > must not disclose or use the information in it. Please notify the sender by
> > > > return email and delete it from your system. Any personal information in
> > > > this email must be handled in accordance with the Privacy Act 1988 (Cth).
> > > >
> > > >
> > > > *************************************************************************
> > > >
> > > > _______________________________________________
> > > > 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
> > > >
> > > >
> > >
> >
> >
> > ************************************************************************
> >
> > Allens Arthur Robinson online: http://www.aar.com.au
> >
> > This email is confidential and may be subject to legal or other
> > professional privilege. It is also subject to copyright. If you have
> > received it in error, confidentiality and privilege are not waived and you
> > must not disclose or use the information in it. Please notify the sender by
> > return email and delete it from your system. Any personal information in
> > this email must be handled in accordance with the Privacy Act 1988 (Cth).
> >
> >
> > *************************************************************************
> >
> > _______________________________________________
> > 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
> >
> >
>
>
> ************************************************************************
>
> Allens Arthur Robinson online: http://www.aar.com.au
>
> This email is confidential and may be subject to legal or other
> professional privilege. It is also subject to copyright. If you have
> received it in error, confidentiality and privilege are not waived and you
> must not disclose or use the information in it. Please notify the sender by
> return email and delete it from your system. Any personal information in
> this email must be handled in accordance with the Privacy Act 1988 (Cth).
>
> *************************************************************************
>
> _______________________________________________
> 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20080224/02adcc5c/attachment.html>


More information about the Ironpython-users mailing list