[Modules] Check if class implements interface

October 2nd, 2011 by damian Leave a reply »

Problem:

I have module that implements my interface:

class MyModule extends ModuleBase implmenets IMyInterface
{
...
}

After loading module from external file:

var moduleInfo:IModuleInfo = ModuleManager.getModule('module_path.swf');
moduleInfo.addEventListener(ModuleEvent.READY, this_READY );
moduleInfo.load(null,null,null ,FlexGlobals.topLevelApplication.moduleFactory);

And creating module object:

private function this_READY( moduleEvent:ModuleEvent ):void {
var moduleInfo:IModuleInfo = moduleEvent.module;
var m:Object = moduleInfo.factory.create();
}

The expression:

m is IMyInterface;

returns false instead of true.

 

Solution:

The solution for this problem is following:

After creating the module object using factory, we must use the expression presented above. I do not know why, but after the first use of this expression or casting (operator as) it behaves right.

Therefore we must add the following line:

private function this_READY( moduleEvent:ModuleEvent ):void {
var moduleInfo:IModuleInfo = moduleEvent.module;
var m:Object = moduleInfo.factory.create();

m is IMyInterface;
}

It looks like error in Flex for me, as I cannot find an explanation for it.

Advertisement

Leave a Reply

Flexmaniaks on Facebook