[IronPython] Calling explicitly-implemented interface methods

Michael Foord michael at voidspace.org.uk
Sat May 1 00:20:38 CEST 2010


Hey all,

I'm porting the dotnet-integration document that comes with IronPython 
to Try Python. The following example doesn't work, because RegistryKey 
isn't available on Silverlight. Can anyone suggest a good alternative of 
an explicitly implemented interface method on a class in Silverlight?

\.NET allows a method with a different name to override a base method
implementation or interface method slot. This is useful if a type implements
two interfaces with methods with the same name. This is known as
`explicity implemented interface methods
<http://msdn.microsoft.com/en-us/library/4taxa8t2.aspx>`_.
For example, `Microsoft.Win32.RegistryKey`
implements `System.IDisposable.Dispose` explicitly::

 >>> from Microsoft.Win32 import RegistryKey
 >>> clr.GetClrType(RegistryKey).GetMethod("Flush") #doctest: +ELLIPSIS
<System.Reflection.RuntimeMethodInfo object at ... [Void Flush()]>
 >>> clr.GetClrType(RegistryKey).GetMethod("Dispose")
 >>>

In such cases, IronPython tries to expose the method using its simple name -
if there is no ambiguity::

 >>> from Microsoft.Win32 import Registry
 >>> rkey = Registry.CurrentUser.OpenSubKey("Software")
 >>> rkey.Dispose()

However, it is possible that the type has another method with the same name.
In that case, the explicitly implemented method is not accessible as an 
attribute.
However, it can still be called by using the unbound class instance 
method syntax::

 >>> rkey = Registry.CurrentUser.OpenSubKey("Software")
 >>> System.IDisposable.Dispose(rkey)


All the best,

Michael Foord

-- 
http://www.ironpythoninaction.com/
http://www.voidspace.org.uk/blog

READ CAREFULLY. By accepting and reading this email you agree, on behalf of your employer, to release me from all obligations and waivers arising from any and all NON-NEGOTIATED agreements, licenses, terms-of-service, shrinkwrap, clickwrap, browsewrap, confidentiality, non-disclosure, non-compete and acceptable use policies (”BOGUS AGREEMENTS”) that I have entered into with your employer, its partners, licensors, agents and assigns, in perpetuity, without prejudice to my ongoing rights and privileges. You further represent that you have the authority to release me from any BOGUS AGREEMENTS on behalf of your employer.





More information about the Ironpython-users mailing list