ILVisualizer 2010 Solution

The projects are set to be built against the .NET 4.0. ILVisualizer2010.zip


Late-bound Array SetValue

The type “System.Array” provides us a set of API for the late-bound array operations, including array creation (Array.CreateInstance), read/write access to array element (Array.SetValue/Array.GetValue). They are convenient to use. Let me start with some code to create an one-dimensional integer array, and set the first 2 elements using early-bind assignment and Array.SetValue, respectively. To simplify…


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

Turn MethodInfo to DynamicMethod

I do not know why anyone ever need this 🙂 but few readers did ask me similar questions before. Solving this problem also demonstrates one more ILReader usage. To build a DynamicMethod, we can choose either DynamicILGenerator or DynamicILInfo. My first impression of using DynamicILGenerator to emit code is slow: you need make calls to…

2

System.Reflection-based ILReader

Compared to what I posted previously here (or what was used in the DynamicMethod visualizer), this new version introduced the Visitor pattern. A do-nothing visitor ILInstructionVisitor is included; the users can focus on their domain-specific logic by simply inheriting from it and overriding few methods. public abstract class ILInstructionVisitor {    public virtual void VisitInlineBrTargetInstruction(InlineBrTargetInstruction inlineBrTargetInstruction)…

2

Always Peverify IL Code of your Dynamic Method

Current dynamic method implementation does not have built-in support to pre-check whether the dynamic method code is verifiable. With bad IL sequence, very often you will get “System.InvalidProgramException: Common Language Runtime detected an invalid program” when it gets executed. See the code example below, where C1, C2 are 2 simple reference types. Btw I understand…


ILGenerator.EmitCall Mainly For vararg Methods

Vararg (variable arguments) methods accept argument lists of unknown length and type. CLR supports this by the IL instruction (arglist) and other BCL types, such as System.ArgIterator. C# compiler has undocumented keyword “__arglist” to support defining vararg methods, accessing variable arguments and calling them, as shown below. static void VarargMethod(string headline, __arglist) {  ArgIterator ai…


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

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