【问题标题】:AS3: Detect Read-Only PropertiesAS3:检测只读属性
【发布时间】:2011-04-04 12:24:03
【问题描述】:

我有一个简单的 AS3 类,它只保存私有变量。每个私有变量都有一个 getter 函数,但并不是所有的私有变量都有 setter 函数。在运行时,有没有办法告诉哪些属性没有设置器但是只读的?然后我可以决定给用户一个输入字段来编辑具有设置器的属性。

【问题讨论】:

    标签: apache-flex flash actionscript-3


    【解决方案1】:

    将任何对象传递给 describeType 都会返回包含有关对象的非常详细信息的 XML。要知道它是否是只读的,您可以访问以下 xml 节点,

    xmlReturnedFromDescType.accessor.access

    这将是三个之一,即 - 只读、只写和读写。

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      我建议describeTypetry..catch

      【讨论】:

        【解决方案3】:

        如前所述,您可以使用 describeType() 将返回一个 XML 对象的所有类型信息。但是,如果您不习惯使用 XML 数据,可以使用 AS3Commons-Reflect 的反射 API:http://www.as3commons.org

        这是一个例子:

        var type:Type = Type.forClass(MyClass);
        
        for each (var accessor:Accessor in type.accessors) {
          if (accessor.writeable) {
            // do something with writeable property
          }
        }
        

        【讨论】:

          【解决方案4】:

          另外,如果您碰巧对 describeType 进行了很多调用,您应该考虑使用 DescribeTypeCache 来提高性能(以及速度)

          【讨论】:

            【解决方案5】:

            这是一个从 describeType 中找出对象是否具有 getter 或 setter 的示例。

            keywords : 反射检测 getter setter discover

            import flash.utils.describeType;
            
            public static function hasSetter( subject : *, propName : String ) : Boolean
            {
                var desc : XML = describeType( subject );
                var access : String = desc.accessor.(@name == propName).@access.toString();
                return ( access.indexOf( "write" ) > -1 );
            }
            
            public static function hasGetter( subject : *, propName : String ) : Boolean
            {
                var desc : XML = describeType( subject );
                var access : String = desc.accessor.(@name == propName).@access.toString();
                return ( access.indexOf( "read" ) > -1 );
            }
            

            和 tousdan 的观点 - 如果你要做很多,那么创建一个缓存。 如果不需要,我不喜欢包含 mx 库,所以我制作了这个简单的缓存:

            public static function getTypeDescription( instance : * ) : XML
            {
                var key : String = getSimpleClassName( instance );
            
                switch( true )
                {
                    case ( typeDescriptions == null ) :
                        typeDescriptions = new Object();
                        typeDescriptions[ key ] = describeType( instance );
                        break;
                    case ( typeDescriptions[ key ] == null ) :
                        typeDescriptions[ key ] = describeType( instance );
                        break;
                    case ( typeDescriptions[ key ] != null ) :
                        // do nothing
                        break;
                    default :
                        trace( "\tERROR : unhanded case DataUtils.getTypeDescription." );
                        return null;
                        break;
                }
            
                return typeDescriptions[ key ] as XML;
            }
            
            public static function getSimpleClassName( instance : * ) : String
            {
                var className : String = String( getClass( instance ) );
                className = className.substring( 7, className.length - 1 );
                return( className );
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2010-11-22
              • 2014-08-22
              相关资源
              最近更新 更多