CLR Array to AX List

If you use any amount of CLRInterop programming in Dynamics AX you have probably at some point or another needed to use X++ code to interact with an array object passed back from a bit of CLR code.

I created the following static class to ease the process of converting a CLRArray object into a X++ style list object.

It only requires two parameters to be passed for the conversion:

  1. CLRObject _input – this is the object array you need converted.
  2. Types _type – this is the X++ type that you want the individual CLR objects converted to.
static List CLRArrayToList(CLRObject _input, Types _type)
    System.Collections.ArrayList list;

    List l = new List(_type);
    AnyType x;

    int cnt, i;
    new InteropPermission(InteropKind::ClrInterop).assert();
        list = new CLRObject('System.Collections.ArrayList',_input);

        cnt = list.get_Count();

        for(i = 0; i < cnt;i++)
            x = CLRInterop::getAnyTypeForObject(list.get_Item(i));
        l = new List(_type);

    return l;

To utilize the method add it to a class (we will name it QuickTools for this example) and call it like this:

CLRObject   bytes;
List        intList = new List(Types::Integer);

// Some code here to populate the bytes object with numeric values
intList = QuickTools::CLRArrayToList(bytes,Types::Integer);
// intList now contains the values stored in bytes

Using this example and a bit of creativity you could create a static method to perform the opposite action and convert a X++ list into a CLR Array object

This entry was posted in AX 2009, AX 2012, AX 4.0, Microsoft Dynamics AX and tagged , , , , . Bookmark the permalink.

One Response to CLR Array to AX List

  1. Pingback: Calculating a File Hash | SLC Consulting, LLC

Comments are closed.