Managed enumeration types and X++


Currently it is painful to use managed enumeration types in X++, although not impossible,


This example came up the other day: A user needed to access an enumeration type called PsDirectoryFilterEnum in a namespace called ProductStudio. He wanted to assign the value PsDirectoryFilterEnum.psDirectoryFilterPSProducts to some variable.


Currently the only wat of doing this is by using the following code using parseClrEnum supplying both the name of the enumeration type and the enumeration literal as strings:


 ProductStudio.PsDirectoryFilterEnum psDirEnum =
       
ClrInterop::parseClrEnum(‘ProductStudio.PsDirectoryrFilterEnum’, ‘psDirectoryFilterPSProducts’);


At some point in the future we’ll probably allow the user to declare enumerations more naturally, like


ProductStudio.PsDirectoryrFilterEnum enum =
    ProductStudio.PsDirectoryrFilterEnum::psDirectoryFilterPSProducts;


using the familiar double colon syntax to access the enumeration literal, just as if it had been a normal X++ enumeration.


 


Comments (1)

  1. keburek says:

    Well, Peter let me spend a whole day figuring this one out, but that just makes it a lesson well-learned.

    Although the double-colon syntax is supported for managed enumeration types in Ax 6.0 X++, ClrInterop::ParseClrEnum can be used to accomplish another crucial scenario.

    Since you cannot bitwise-or CLRObjects in X++, it is a challenge to combine .NET Enum flags in X++ code.  You can hack around by casting your enumeration values to x++ native int's, using '|' on those, and casting back, but it's all very ugly and isn't even the end of the story.

    Using ClrInterop::ParseClrEnum, however:

    <code>

    //privateStatic:

    // type == System.Reflection.BindingFlags

    // value == (Static, NonPublic) (40)

    System.Reflection.BindingFlags privateStaticFlag = ClrInterop::ParseClrEnum("System.Reflection.BindingFlags","Static, NonPublic");

    </code>

    Bitwise OR achieved!