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:
- CLRObject _input – this is the object array you need converted.
- 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();
try
{
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.addEnd(x);
}
}
catch
{
l = new List(_type);
}
CodeAccessPermission::revertAssert();
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
没有评论:
发表评论