Traversing elements in an IEnumerable from X++

This issue recently came up: How do you traverse the elements in an instance of an IEnumerable<T> from X++?

The answer is not as simple as it should be. The problem is that the X++ language does not currently support generics as a firstclass part of the managed X++ story. This means that we need to do some tricks to make this work. At least it shows off the capabilities of the ClrObject class: It can take a string that designates the type that you want to create an instance of. The CLR runtime system is so cleverly designed that it allows decorating types so they designate generic types. This is done by appending the backtick character, followed by the number of generic arguments, enclosed by the argument types enclosed in angular brackets.

So, the type


is  represented as


With this knowledge the task is not hard:


 static void GenericTypeDemo(Args _args) 
    ClrObject o = new ClrObject("System.Collections.Generic.List`1[System.Int32]"); 
    ClrObject enumerator; 
    int theValue; 
    // o is an instance of an IEnumerable<int> 
    print o.ToString(); 
    enumerator = o.GetEnumerator(); 
    while (enumerator.MoveNext()) 
        theValue = enumerator.get_Current(); 
        print theValue;