[IronPython] C#, Types and IronPython

Nicholas Riley njriley at uiuc.edu
Sun Mar 11 06:49:50 CET 2007

On Sat, Mar 10, 2007 at 01:34:32PM +0000, Michael Foord wrote:
> I have some C# that is getting at type objects in several different 
> ways, and I would like to know the difference.
> Type.GetType("int")
> typeof(int)
> and I think :
> int.GetType()

In IronPython this is asking for the type of 'int' here, not the type
of _an_ int.  In C# it does both.

> What are the differences in semantics between 'typeof(...)' and 
> 'Type.GetType(...)' ?

There should be none; however, as you saw in another post, you have to
provide qualified type names to Type.GetType.  For example, this works

>>> import clr; from System import *
>>> Type.GetType('System.String')
<System.RuntimeType object at 0x000000000000002C [System.String]>
>>> 'foo'.GetType()
<System.RuntimeType object at 0x000000000000002C [System.String]>

as does this:

>>> (5).GetType()
<System.RuntimeType object at 0x000000000000002D [System.Int32]>

IronPython has its own definition of types, which isn't the same as
the CLR types.  The IronPython runtime creates objects which conform
to an "IPythonType" interface to represent its own type system.

If you need to get a Pythonic view of type objects you'll have to
mirror the logic you find in IronPython.Runtime.Operations.Ops.GetAttr
(in C#), or the weird way the single-parameter 'type' constructor
works as if it's a function.

You can see the mapping as follows:

>>> from IronPython.Runtime.Operations.Ops import *
>>> GetDynamicType(5)
<type 'int'>
>>> GetDynamicType('foo')
<type 'str'>
>>> GetDynamicType(Type)
<type 'type'>

Nicholas Riley <njriley at uiuc.edu> | <http://www.uiuc.edu/ph/www/njriley>

More information about the Ironpython-users mailing list