Flex动态获取Object的属性和值以及完成Object之间value的拷贝

1.如何动态获取Object中的属性和值
var ct:CustomObject=newCustomObject(); 
var xml:XML = describeType(ct);
for each(var accessor in xml..accessor){
  
var name:String= accessor.@name;
  
var type:String= accessor.@type;
  trace(ct[name]);
}

2. 对象之间的拷贝
/**
 * copies a source object to a destination object
 * @param sourceObject the source object
 * @param destinationObject the destination object
 *
 
*/
public static 
function copyObject(sourceObject:Object, destinationObject:Object):void
{
    
// check if the objects are not null
    if((sourceObject) && (destinationObject)) {
        
try
        {
            
//retrive information about the source object via XML
            var sourceInfo:XML = describeType(sourceObject);
            
var objectProperty:XML;
            
var propertyName:String;

            
// loop through the properties
            for each(objectProperty in sourceInfo.variable)
            {
                propertyName 
= objectProperty.@name;
                
if(sourceObject[objectProperty.@name] != null)
                {
                    
if(destinationObject.hasOwnProperty(objectProperty.@name)) {
                        destinationObject[objectProperty.@name] 
= sourceObject[objectProperty.@name];
                    }
                }
            }
            
//loop through the accessors
            for each(objectProperty in sourceInfo.accessor) {
                
if(objectProperty.@access == "readwrite") {
                    propertyName 
= objectProperty.@name;
                    
if(sourceObject[objectProperty.@name] != null)
                    {
                        
if(destinationObject.hasOwnProperty(objectProperty.@name)) {
                            destinationObject[objectProperty.@name] 
= sourceObject[objectProperty.@name];
                        }
                    }
                }
            }
        }
        
catch (err:*) {
            ;
        }
    }



posted on 2010-08-25 13:55 想飞就飞 阅读(5092) 评论(0)  编辑  收藏 所属分类: Flex


只有注册用户登录后才能发表评论。


网站导航:
 

公告


导航

<2010年8月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
2930311234

统计

常用链接

留言簿(13)

我参与的团队

随笔分类(69)

随笔档案(68)

最新随笔

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜