开发者

Flex/AS3 : Automatically instantiate package classes in an array (plugin classes)

开发者 https://www.devze.com 2022-12-13 22:10 出处:网络
This is my first time here, but I already found some good answers here, so I\'d like to thank everyone.

This is my first time here, but I already found some good answers here, so I'd like to thank everyone.

I'm developping a small Flex application and I want to instantiate every class from a package into an array, so I can parse it afterwards. To clarify开发者_高级运维, I'm trying to ease a plugin management system for my application, with the old canProcess/doProcess routine : My plugins are all in ONE package, including an abstract plugin class. First, I create one instance of every classes in this package (that's where I need help) and put them in an array. Then, whenever I need a plugin for an item, I parse every plugin class in my array with the canProcess method (the item is the parameter). If one plugin says yes, then I send the item to the doProcess method and stop parsing the array.

I know I could implement by hand every class in my package, but I'd prefer not bothering to do it.

Has anyone an idea ?

Thx


AS3 reflection doesn't allow you to list all classes in a package. You will have to write the class names to an (xml) file at the server, load it and then use getDefinitionByName to get Class objects from those strings and then instantiate them.

Consider the sample xml file:

<root package="boris.ratak">
  <className>Plugin1</className>
  <className>Plugin2</className>
  <className>Plugin3</className>
</root>

load it with URLLoader and parse it like:

import flash.utils.getDefinitionByName;

var pack:String = String(xml.@package) + ".";
for each(var cl:String in xml.className)
{
   var name:String = pack + String(cl.text());
   var Type:Class = getDefinitionByName(name) as Class;
   pluginArray.push(new Type());
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号