R – Flex 3.2 casting problem

actionscript-3apache-flexcasting

I'm newish to flex an have recently hit a snag casting.

this is the code I'm running.

/**
* return background definition depending on time
*/ 
private function findBackgroundItem( relativeTime : Number ) : CSBackgroundItem
{
    if( this.backgroundItems == null ) return null;
    var result :CSBackgroundItem = null;
    var relative:Date = new Date(relativeTime);
    for( var i : Number = 0; i < this.backgroundItems.length; i++ )
    {
        // backgroundItems is an Ilist of CSBackgroundItem.
        var colourItem : CSBackgroundItem = CSBackgroundItem( this.backgroundItems.getItemAt( i ) );

        // other stuff here
    }           
    return result;
}

The problem occurs when the IList.getItemsAt() result is cast to the CSBackgroundItem variable colourItem. The following error is thrown

TypeError: Error #1034: Type Coercion failed: cannot convert com.mystuff::CSBackgroundItem@650e8dd1 to com.mystuff.CSBackgroundItem.

If I use the 'as' keyword I get cast results in the colourItem being null. Using the debugger shows that the list is not empty and is indeed populated with CSBackgroundItem objects.

Now this is the wacky bit.. this code works, the first time the module it's in loads.. subsequent loads (after unloading it) throw the exception.

Can anyone shed any light on why this might happen?

Best Answer

FYI, a type loaded into a child ApplicationDomain can be cast to a type (that it extends/implements) in the parent ApplicationDomain.

Eg.

loader.applicationDomain = ApplicationDomain.currentDomain; // parent domain
loader.applicationDomain = new ApplicationDomain(ApplicationDomain.currentDomain); // child domain
loader.applicationDomain = new ApplicationDomain(); // new domain