[IronPython] Type.GetType returns None

Michael Foord fuzzyman at voidspace.org.uk
Sun Mar 11 15:35:09 CET 2007

J. Merrill wrote:
> Those aren't comparable statements.  If you had passed "String" (in quotes) in your C# I think that would have worked, but "string" should not -- 
In the example I gave, 'string' was a variable name, not a literal. :-)

There are overloads that allow you to specify a case-insensitive search 
as well by the way.

The trouble I have is that in IronPython 'Type.GetType("String")' 
returns None. In fact all the following return None :

 >>> Type.GetType("String")
 >>> Type.GetType("int")
 >>> Type.GetType("Int32")

Even when I move the 'Type.GetType' call into C# and then call it from 
IronPython it still returns None - meaning it couldn't find the type I 
was asking for. I don't understand why...

Adding the assembly qualifier (System.String) works - but they are used 
without the qualifier in an MSDN example, which works when compiled as C#...

Using "some string".GetType() (with a string literal) works - but is a 
little inconvenient.

Also - the C# examples use 'typeof(int)' and 'Type.GetType("int")'. To 
make it work I've replaced these with 'Int32', which works but is 
obviously not exactly the same...



> the Type.GetType(string) overload is defined as a case-sensitive search, and in general wants an "assembly qualified name".
> http://msdn2.microsoft.com/en-us/library/system.type.assemblyqualifiedname.aspx
> Perhaps all would be fine in pure IP if you had just used Type.GetType(String) or String.GetType(); I'm away and not easily able to try it myself.
> I would guess that the cause is that Python's "string" type (lowercase without the quotes) is not the same as .Net's "String" type (mixed case without quotes).
> At 09:48 AM 3/10/2007, Michael Foord wrote
>> Michael Foord wrote:
>>> Hello all,
>>> Why does "Type.GetType(string)" return None from IronPython ?
>>> I need to use it with names like "Console", "int" and "string" - 
>>> fetching the .NET type objects. I guess this is because of the reflected 
>>> types ? Do I have an alternative that will work ? (other than stub C#).
>> Interesting - even with the following C# called from IronPython it 
>> returns None. The code I am translating is from the MSDN docs. Any clues ?
>> namespace SimpleTest
>> {
>>    public class SimpleTest
>>    {
>>        public static Type getType(string typename){
>>            return Type.GetType(typename);
>>        }
>> }
>> Michael Foord
>> http://www.voidspace.org.uk/ironpython/
> J. Merrill / Analytical Software Corp
> _______________________________________________
> 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