Wednesday, November 25, 2009

Enumerating with CLR Interop

Like seen in previous posts on this blog, with CLR Interop, a whole world of programming libraries are available to Ax.
But these come with little annoyances as well. Like when you try to use an enum in your programming library.
As an example, take a look at following code:

static void ClrEnum(Args _args)
{ System.Drawing.Image myImage;
System.Drawing.RotateFlipType myRotateFlipType;
;
myImage = System.Drawing.Image::FromFile(@'C:\MyBitmap.bmp');
myRotateFlipType=System.Drawing.RotateFlipType::Rotate90FlipNone();
myImage.RotateFlip(myRotateFlipType);
myImage.Save(@'C:\NewBitmap.bmp');
}

Everything looks OK. When typing in the code, IntelliSense even added the enumerations, but now if fails to compile. Saying

The class System.Drawing.RotateFlipType does not contain this function.

When working with CLR Interop, you might get this error from time to time. You check and doublecheck, there is no typing error.

So this wouldn't be much of a blog post if it didn't offer some workaround.

You can use the ClrInterop::parseClrEnum command.
With this command, you can convert a string value of an enumerated constant to an equivalent CLRObject instance.

Like this:

myRotateFlipType=ClrInterop::parseClrEnum('System.Drawing.RotateFlipType','Rotate90FlipNone');

Now that compiles! So now, enumerations in assemblies won't bother you again.

No comments:

Post a Comment