[IronPython] howto access interface method?

Jim Hugunin Jim.Hugunin at microsoft.com
Fri Feb 17 18:32:55 CET 2006


Dino's right that this is behavior that we'd like to make more natural and that we'll look into fixing.

However, there's a classic python-style work-around for this issue that can also be used to handle the case where a class implements two conflicting interfaces.  This relies on the fact that methods are first class entities in Python.  BTW - I'd suggest that you use shorter form names in actual code:

>>> import clr
>>> clr.AddReferenceByPartialName("System.Windows.Forms")
>>> import System
>>> dgv = System.Windows.Forms.DataGridView()
>>> dgv.BeginInit() #fails
Traceback (most recent call last):
  File , line 0, in input##102
AttributeError: 'DataGridView' object has no attribute 'BeginInit'
>>> System.ComponentModel.ISupportInitialize.BeginInit(dgv) #works
>>>

This is the same idiom that Python uses for explicitly calling a super classes method on a subclass and can be used here to call an interface method on an object that explicitly implements and interface but doesn't expose the methods directly.

While we'll try to do the right thing for you behind the scenes more often, this idiom is valuable to know as the "true" way to call these kinds of methods.  This same approach can also be used sometimes with COM objects when we fail to do the right thing for you automagically.

-Jim

-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Dino Viehland
Sent: Thursday, February 16, 2006 8:56 AM
To: Discussion of IronPython
Subject: Re: [IronPython] howto access interface method?

The issue here is that the interface's methods are marked private, but are exposed publicly via the interface (aka explicit interface implementation) -hence the reason you need the cast in C#.

This is a shortcoming we're aware of but we don't have a bug on it.  I'll go ahead and file a bug on this so it doesn't get lost.  The fix may be a little complex for this one so it may not make it for beta 4 but I'll initially open it as a beta 4 bug so we'll give it a shot.


-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Chee Meng
Sent: Thursday, February 16, 2006 12:58 AM
To: users at lists.ironpython.com
Subject: [IronPython] howto access interface method?


hi,

I am using the DataGridView object which implements the interface ISupportInitialize,
and that have 2 methods BeginInit and EndInit .

However, through Ironpython,
the 2 methods cannot be found.

is there a way to access the 2 methods in python?

#((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.dataGridView1.BeginInit()

above code gives this error, AttributeError: 'DataGridView' object has no attribute 'BeginInit'


thks


regards
cheemeng




_______________________________________________
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