[IronPython] Reflection.Emit from IronPython: What is the equivalent of typeof() in C# ?

Robert Smallshire robert at smallshire.org.uk
Fri Aug 7 20:46:51 CEST 2009


Hello,

Here is the solution to my problem, which turns out to be as much a silly
mistake on my part as to how to use the Reflection.Emit API, as it is an
IronPython conundrum.

It turns out the array type for the field, that I was seeing in Reflector
and IL Disassembler was is set by the DefineField call (of course!), rather
than the Emit(Newarr, type) call which simply puts a reference to the new
array on the stack.  The original how-to-do-typeof question still applies,
since this method also takes a type parameter:

Now I've made the change in the right place in my code, I can report that
the following is known to work, a gives a String[] as you would expect in
the generated assembly:

type_builder.DefineField("foo",
System.Type.GetType('System.String').MakeArrayType(1),
FieldAttribute.Private)

For the record, the following expressions supplied by me and others also
work:

str().GetType().MakeArrayType(1)
System.String().GetType().MakeArrayType(1)
clr.GetClrType(System.String).MakeArrayType(1)
clr.GetClrType(type('')).MakeArrayType(1)

Thank you to everybody for your correct answers, now I've figured out how to
use them, and apologies for any confusion I may have caused by prematurely
rejecting your answers before I'd got my act together.  It seems there are
many equivalents of C# typeof in IronPython.

Thanks,

Rob

> -----Original Message-----
> From: users-bounces at lists.ironpython.com 
> [mailto:users-bounces at lists.ironpython.com] On Behalf Of 
> Robert Smallshire
> Sent: 06 August 2009 22:05
> To: 'Discussion of IronPython'
> Subject: [IronPython] Reflection.Emit from IronPython: What 
> is theequivalent of typeof() in C# ?
> 
> 
> Hello,
> 
> I'm attempting to drive the Reflection.Emit API from 
> IronPython. In C# typical Reflection.Emit use makes use 
> typeof(...) facility in C#, to enable the determination of 
> types without needing an instance of that type.
> 
> For example, to create an array of .NET CTS Strings in IL 
> from C# one might
> do:
> 
> generator.Emit(OpCodes.Newarr, typeof(string));
> 
> where the second argument to Emit is the element type of the array.
> 
> I've tried various alternatives from IronPython, including
> 
> generator.Emit(OpCodes.Newarr, System.Type.GetType('System.String'))
> generator.Emit(OpCodes.Newarr, str().GetType()) 
> generator.Emit(OpCodes.Newarr, System.String().GetType()) 
> generator.Emit(OpCodes.Newarr, clr.GetClrType(System.String))
> 
> however, all of these result in RuntimeType[] rather than 
> String[] in the generated CIL.
> 
> How do I get typeof(System.String) from IronPython?
> 
> Rob
> 
> Robert Smallshire
> robert at smallshire.org.uk
> http://smallshire.org.uk/
> Currently in Norway (UTC +2 hours) 
> 
> _______________________________________________
> 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