Re: [IronPython] Reflection.Emit from IronPython: What is the equivalent of typeof() in C# ?
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-boun...@lists.ironpython.com [mailto:users-boun...@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 rob...@smallshire.org.uk http://smallshire.org.uk/ Currently in Norway (UTC +2 hours) ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
[IronPython] Reflection.Emit from IronPython: What is the equivalent 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 rob...@smallshire.org.uk http://smallshire.org.uk/ Currently in Norway (UTC +2 hours) ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
Re: [IronPython] Reflection.Emit from IronPython: What is the equivalent of typeof() in C# ?
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 rob...@smallshire.org.uk http://smallshire.org.uk/ Currently in Norway (UTC +2 hours) ___ Users mailing list Users@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@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
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 rob...@smallshire.org.uk http://smallshire.org.uk/ Currently in Norway (UTC +2 hours) ___ Users mailing list Users@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@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
Re: [IronPython] Reflection.Emit from IronPython: What is the equivalent of typeof() in C# ?
Try: clr.GetClrType(type('')) David Fugate Microsoft - IronPython http://knowbody.livejournal.com -Original Message- From: users-boun...@lists.ironpython.com [mailto:users-boun...@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 rob...@smallshire.org.uk http://smallshire.org.uk/ Currently in Norway (UTC +2 hours) ___ Users mailing list Users@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@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
Re: [IronPython] Reflection.Emit from IronPython: What is the equivalent of typeof() in C# ?
You can also just use the type like System.String to avoid having to create an instance of the type. import System import clr clr.GetClrType(System.String) System.RuntimeType object at 0x002B [System.String] -Original Message- From: users-boun...@lists.ironpython.com [mailto:users-boun...@lists.ironpython.com] On Behalf Of Dave Fugate Sent: Thursday, August 06, 2009 2:07 PM To: rob...@smallshire.org.uk; Discussion of IronPython; 'Michael Foord' Subject: Re: [IronPython] Reflection.Emit from IronPython: What is the equivalent of typeof() in C# ? Try: clr.GetClrType(type('')) David Fugate Microsoft - IronPython http://knowbody.livejournal.com -Original Message- From: users-boun...@lists.ironpython.com [mailto:users-boun...@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 rob...@smallshire.org.uk http://smallshire.org.uk/ Currently in Norway (UTC +2 hours) ___ Users mailing list Users@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@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com ___ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com