web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Casting from ArrayList CLRObject to an AX class object

(0) ShareShare
ReportReport
Posted on by

Hey guys,

I need a data structure that allows me to do usual and reversal iteration, so I am using the System.Collections.ArrayList to store elements from my FBGiaRecordChildren_BR class .

My code is running on IL and I am having a Casting problem while trying to do this:

    FBGiaRecordChildren_BR  recordChildren;
    int                     i, size;
    size = recordChildrenList.get_Count();
    for( i = size-1 ; i >= 0  ; i--)
    {
        recordChildren = recordChildrenList.get_Item(i);
        
        if (recordChildren.getRecord() == _parentType)
        {
            return recordChildren;
        }
    }
Error: System.InvalidCastException: Unable to cast object of type 'Microsoft.Dynamics.AX.ManagedInterop.Object' to type 'Dynamics.Ax.Application.FBGiaRecordChildren_BR'.
I've already tryed to use CLRInterop::getAnyTypeForObject, but doesn't help me:

    FBGiaRecordChildren_BR  recordChildren;
    CLRObject               obj;
    int                     i, size;
    size = recordChildrenList.get_Count();
    for( i = size-1 ; i >= 0  ; i--)
    {
        recordChildren = recordChildrenList.get_Item(i);
        
        recordChildren = CLRInterop::getAnyTypeForObject(obj);
        
        if (recordChildren.getRecord() == _parentType)
        {
            return recordChildren;
        }
    }
I would be very thankful if anyone could help me.
Thanks a lot!

*This post is locked for comments

I have the same question (0)
  • Martin Dráb Profile Picture
    237,882 Most Valuable Professional on at

    The second piece of code still contains the same problem. The assignment recordChildren = recordChildrenList.get_Item(i) is invalid because you can't safely assign any Object to a variable of FBGiaRecordChildren_BR type. There is no guarantee that get_Item(i) returns a type compatible with FBGiaRecordChildren_BR.

    Nevertheless you can cast the value to the right type using the as operator:

    recordChildren = recordChildrenList.get_Item(i) as FBGiaRecordChildren_BR;
    if (!recordChildren)
    {
        throw error(...);
    }
  • Suggested answer
    Philip Leitch Profile Picture
    104 on at

    I faced the same problem. The code worked perfectly in client side, but failed on the AOS Batch server.  

    I tracked the problem down to the variable that comes out when run on the AOS (running on CIL) comes out as a type of Object, whereas it comes out as the correct AX type when run as client.  

    AX can't cast from Object to an AX type and that's why the error happens.  Putting in a Cast doesn't fix it, the same error happens.

    I attempted to change the type of the variable using System.Convert::ChangeType, however the it doesn't have IConvertable interface.

    In other words - we can't do anything about the object once it comes out, however there is a solution: change how the variable goes IN. I replaced both the key and the variable with .Net variables:

    System.String Key;
    System.Object oVar;

    I don't believe the key was necessary but the System.Object on the variable was.  In this instance (below) I'm using a sorted list.

                Key = CurrEmployee.parmEmployeeID();

                oVar = CurrEmployee;

                Employees.Add(Key, oVar);

    By first marshaling the class into a .Net class it gains the appropriate AX type.  If you just pass the class in directly (which works perfectly well in non CIL executed code) it doesn't gain the AX type.

    Once you fix how the data goes in the exception goes away.  From what I've found this is the ONLY way to fix the issue due to the lack of ability to cast or convert the output of Get_Item.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Priya_K Profile Picture

Priya_K 4

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#3
Sukrut Parab Profile Picture

Sukrut Parab 2 Moderator

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans
Liquid error: parsing "/globalsearch/?q=quote+of+the+day.+++399118" - Nested quantifier +.