[IronPython] Using CLR bound COM interfaces
Shri Borde
Shri.Borde at microsoft.com
Mon Feb 26 20:59:55 CET 2007
Using isinstance should be the right way to check if an object is an interface. However, this does not currently work. http://www.codeplex.com/WorkItem/View.aspx?ProjectName=IronPython&WorkItemId=8121 tracks this issue. The (admittedly ugly) workaround that is also mentioned in the bug is:
>>> import clr
>>> import System
>>> isinstance(1, System.IFormattable) # Should return True
False
>>> clr.GetClrType(System.IFormattable).IsAssignableFrom(clr.GetClrType(type(1))) # Workaround
True
For your second question, the answer depends on the actual type (not the statically declared C# type) of "geometryDef". If it implements IGeometryDefEdit explicitly, you do need to use "IGeometryDefEdit.MethodName(geometryDef, args)". Else, you can just use "geometryDef. MethodName(args)". The following bugs are related.
http://www.codeplex.com/IronPython/WorkItem/View.aspx?WorkItemId=4538
http://www.codeplex.com/IronPython/WorkItem/View.aspx?WorkItemId=1506
-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Alex Willmer
Sent: Sunday, February 25, 2007 5:36 PM
To: users at lists.ironpython.com
Subject: [IronPython] Using CLR bound COM interfaces
Hello,
I've been attempting with IronPython, to access the CLR binding to
ArcObjects[1], a COM based Geographic Information System (GIS) library.
I come at this from a CPython background, so my knowledge of COM & .NET
are lacking.
I'm trying to understand how interfaces (IFoobar) are dealt with in
IronPython. A C# example I'm attempting to transcribe contains the
following lines:
if (!((objectWorkspace is IWorkspace) ||
(objectWorkspace is IFeatureDataset)))
{
throw(new Exception(....));
}
My assumption was that isinstance() would replace 'is', but the
following would indicate otherwise. From the .NET 2.0 docs[2] the
System.Uri class inherits from ISerializable
>>> import System
>>> import System.Runtime.Serialization
>>> x = System.Uri('http://example.net')
>>> isinstance(x, System.Uri)
True
>>> isinstance(x, System.Runtime.Serialization.ISerializable)
False
How should I test a particular object implements an interface?
My second query is based on the following C#:
IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit) geometryDef;
// Assign Geometry Definition
geometryDefEdit.GeometryType_2 = geometryType;
geometryDefEdit.GridCount_2 = 1;
geometryDefEdit.set_GridSize(0, 0.5);
geometryDefEdit.AvgNumPoints_2 = 2;
geometryDefEdit.HasM_2 = false;
geometryDefEdit.HasZ_2 = true;
I understand that to transcribe this is I should forget the
assignment/cast and replace geometryDefEdit.Method(args) with
IGeometryDefEdit.MethodName(geometryDef, args). Is this correct?
Many thanks.
Alex Willmer
[1] http://edndoc.esri.com/arcobjects/9.1/
[2] http://msdn2.microsoft.com/en-us/library/system.uri.aspx
_______________________________________________
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