如何列出(潜在的大量)班在一个特定的包? (AS3) [英] How do I list (a potentially large amount of) classes in a specific package? (AS3)

查看:216
本文介绍了如何列出(潜在的大量)班在一个特定的包? (AS3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类在包中,例如X量。 com.trevorboyle.lotsofclasses,我不断加入更多的(这些课程将可能全部是静态的,可能都继承了同一个类)。

I have x amount of classes in a package eg. "com.trevorboyle.lotsofclasses" and I keep adding more (These classes will probably all be static and will probably all extend the same class).

我想创建这些类,$ P $的下拉列表pferably,而无需手动创建一个数组自己。

I wish to create a drop-down list of all these classes, preferably without having to manually create an array myself.

在一类是从列表中选择,我就可以用getDefinitionByName返回它,因为我知道它的名字,在这一点上。

Once a class is selected from the list, I'll be able to use getDefinitionByName to return it, because I will know it's name at this point.

现在的问题是,因为我相信这是在AS3列出所有的类在一个特定的软件包的支持,是否有处理这种设计模式?

The question is, as I believe there is no support in AS3 to list all the classes in a specific package, is there a design pattern that handles this?

推荐答案

有两种方式:

首先,传统的清洁AS3实现:

First, traditional clean as3 implementation:

创建注册你感兴趣的所有类的类的初始化(当它第一次在code使用的)的类。

Create a class that registers all the classes of your interest at class initialization (when it's first used in code).

事情是这样的:

package   {
import flash.utils.getQualifiedClassName;

public class ClassesRegister {

    private static const PACKAGE_RESOLVER_PATTERN   : RegExp = /^(?P<packageName>[a-zA-Z_\.]+)::/;

    private static const registeredClasses : Object = { };

    public static function registerClass ( clazz : Class ) : void {

        var packageName : String = PACKAGE_RESOLVER_PATTERN.exec( getQualifiedClassName( clazz ) ).packageName || "";


        if ( !registeredClasses[ packageName ] ) {
            registeredClasses[ packageName ] = [];
        }

        registeredClasses[ packageName ].push( clazz );

    }

    public static function getClassesOfPackage ( packageName : String ) : Array {

        return registeredClasses[ packageName ].concat();

    }

}

}

登记在初始化类:

package   {

public class SomeClass {

    // Add class to register in static initializer
    {
        ClassesRegister.registerClass( SomeClass );
    }

    /**
     * Class constructor
     */
    public function SomeClass () {

    }

}

}

和另外还有一个,不是由我自己测试,但前景十分看好: 使用 as3commons字节code库

And there is another, not tested by myself but very promising: Use as3commons bytecode library.

类<一href="http://www.as3commons.org/as3-commons-byte$c$c/asdoc/org/as3commons/byte$c$c/reflect/Byte$c$cTypeCache.html#getClassesWithMetadata"相对=nofollow>字节codeTypeCache 地产 definitionNames 商店已在所有字节code遇到的所有完全限定的定义的名称被扫描。你可以只遍历该列表,并采取你所需要的。当然,你必须确保你将需要在运行时所有的类被编译成字节code(你有什么地方初始化这些类或使用AS3编译器指令)。

Class ByteCodeTypeCache in property definitionNames stores all fully qualified definition names that have been encountered in all the bytecode that was scanned. You can just iterate through that list and take what you need. Of course you have to make sure that all classes you will need on runtime is compiled into bytecode (you have to initialize those classes somewhere or use as3 compiler directives).

这篇关于如何列出(潜在的大量)班在一个特定的包? (AS3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆