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

Dino Viehland dinov at microsoft.com
Fri Aug 7 19:45:59 CEST 2009


Is this what you're trying to accomplish?

from System.Reflection import *
from System.Reflection.Emit import *
import System
import clr
clr.AddReference('System.Core')

dm = DynamicMethod("test", clr.GetClrType(System.Array[str]), System.Type.EmptyTypes)
ilgen = dm.GetILGenerator()
ilgen.Emit(OpCodes.Ldc_I4_4)
ilgen.Emit(OpCodes.Newarr, str)
ilgen.Emit(OpCodes.Ret)

dlg = dm.CreateDelegate(System.Func[System.Array[str]])
print dlg()

prints:
Array[str]((None, None, None, None))

Here I'm just passing str as the type directly - there's a conversion defined
from PythonType -> System.Type.  But clr.GetClrType(str) and ''.GetType() both
work fine for me (on 2.0.1 and 2.6B2).

> -----Original Message-----
> From: users-bounces at lists.ironpython.com [mailto:users-
> bounces at lists.ironpython.com] On Behalf Of Robert Smallshire
> Sent: Friday, August 07, 2009 10:35 AM
> To: 'Discussion of IronPython'
> Subject: [IronPython] FW: Reflection.Emit from IronPython: What is
> theequivalent of typeof() in C# ?
>
> Hi David,
>
> > Try:
> >     clr.GetClrType(type(''))
>
> This still produces RuntimeType[] in the generated CIL :-(
>
> Thanks for trying!
>
> Rob
> >
> >
> >
> > >
> > >
> > > David Fugate
> > > Microsoft - IronPython
> > > http://knowbody.livejournal.com
> > >
> > > -----Original Message-----
> > > From: users-bounces at lists.ironpython.com
> > > [mailto:users-bounces at lists.ironpython.com] On Behalf Of
> > > Robert Smallshire
> > > Sent: Thursday, August 06, 2009 1:53 PM
> > > To: 'Michael Foord'; 'Discussion of IronPython'
> > > Subject: Re: [IronPython] Reflection.Emit from IronPython:
> > > What is the equivalent of typeof() in C# ?
> > >
> > > Hi Michael,
> > >
> > > I'm afraid not:
> > >
> > > >>> clr.GetClrType('')
> > > TypeError: expected Type, got str
> > >
> > > My hopes were momentarily raised when I discovered that your
> > > otherwise very comprehensive book mentions typeof in the
> > > index (page 398) - but then I found that you only use it from C#.
> > >
> > > Cheers,
> > >
> > > Rob
> > >
> > > >
> > > > clr.GetClrType('') ? (on an instance of a string)
> > > >
> > > > Michael
> > > >
> > > > Robert Smallshire wrote:
> > > > > 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
> > > > >
> > > >
> > > >
> > > > --
> > > > http://www.ironpythoninaction.com/
> > > > http://www.voidspace.org.uk/blog
> > > >
> > > >
> > >
> > > _______________________________________________
> > > Users mailing list
> > > Users at lists.ironpython.com
> > > http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
> > >
> >
>
> _______________________________________________
> 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