Activator.CreateInstance and beyond

Q: Assume we have 2000 unknown types; (however) we know each type has a constructor with integer as its’ only parameter type. How to create objects 10000 times for each type (and make such late-new fast)? Activator.CreateInstance comes to my fingers first. It is just so convenient to use: calling Activator.CreateInstance(type, new object[] {100}) in…

12

DebuggerVisualizer for DynamicMethod (Show me the IL)

Have you ever tried DynamicMethod, one of the coolest features in the .NET 2.0? Hope you have; otherwise, you can try it now with the examples 1, 2. Like emitting IL to MethodBuilder, we first get ILGenerator from a DynamicMethod, and make a series of ILGenerator.Emit calls to emit the IL instructions. If the emit…

11

Member Order Returned by GetFields, GetMethods …

As John mentioned in his post, every Reflection user should keep this in mind: our code should not count on the member order returned by GetFields, GetMethods and other similar GetXXXs calls. Given two fields (F1, F2) in one type, I think the compiler is free to emit F1 before F2, or vice versa. Also…

9

VisualStudio as my IronPython editor

The following steps are what I did to get Visual Studio ready as my IronPython (and IronRuby) editor. Install the latest internal dogfood build of Visual Studio 2008. you may use Visual Studio 2005 or download the VS 2008 public beta2; Download and install ASP.NET futures release (July 2007). This will give me the nice…

8

Type.GetType(string typeName) returns null !?

Type.GetType gives us the ability to get type back from a string. We pass a well-written type name string to it, and expect it return that type. Sometimes, to your surprise, it returns null. For example, Type.GetType(“System.Data.SqlClient.SqlException”). If the assembly name is specified in the typeName string, Type.GetType() will search inside this assembly only; otherwise,…

7

Take Two: IL Visualizer

I was glad to hear many positive feedbacks about the DebuggerVisualizer for DynamicMethod; on the sad side, it shows our lack of good LCG debugging support (on which, Mike Stall is seeking your opinion). Recently along with the ILReader update, I made a few changes on the visualizer: Add support to show IL of RuntimeMethodInfo…

6

ILVisualizer VS2008 solution

I attached the ILVisualizer VS2008 solution in this post. There is no source code update; the only change is to upgrade the Microsoft.VisualStudio.DebuggerVisualizers.dll reference from version 8.0.0.0 to version 9.0.0.0. Just for your convenience, so you can see a successful build after downloading in VS2008. Thanks Julien for leaving the instruction here. ILVisualizer2008.zip

5

IronPython: clr.AddReference

In order to interop with .NET libraries, we need first load in the assemblies we want to play with. The family of AddReference methods in the clr module serves the purpose. clr.AddReference clr.AddReferenceByName clr.AddReferenceByPartialName clr.AddReferenceToFile clr.AddReferenceToFileAndPath clr.AddReference accepts System.Reflection.Assembly objects and/or assembly names. The typical usage looks like: asm = … # any approach of…

5

Hello World with DynamicILInfo

The following "Hello World" program shows how to use APIs in the class DynamicILInfo. MSDN documentation will have more details on this class in the next release. using System; using System.Reflection; using System.Reflection.Emit;   class Demo {   static void Main() {     DynamicMethod dm = new DynamicMethod("HelloWorld", typeof(void), Type.EmptyTypes, typeof(Demo), false);     DynamicILInfo il…

5

Reflection and Nullable<T>

You might have already read this: VS2005 made the last-minute DCR related to boxed Nullable<T>. Runtime now treats Nullable<T> differently from other generic value types when boxing: Int32? x = null; object y = x;  // y is simply null, not a truly boxed “nullable<int> struct” (which then tells it has no value) And Int32…

4