AS3 类型强转类
来源:互联网 发布:adobe 2018破解版 mac 编辑:程序博客网 时间:2024/05/08 17:57
还在为类型强转烦愁吗?
看看darron schall 给我们带来的这个强制转换类:
http://www.darronschall.com/weblog/archives/000247.cfm
用法:ObjectTranslator.objectToInstance(Object对象,要强转成的类型) as 要强转成的类型
嘿嘿,这回不用担心Object转VO转不成了。
源码:
package com.goufang.util
...{
import flash.net.ObjectEncoding;
import flash.net.registerClassAlias;
import flash.utils.ByteArray;
import flash.utils.describeType;
import flash.utils.getDefinitionByName;
/** *//**
* Utility class to convert vanilla objects to class instances.
*/
public final class ObjectTranslator
...{
/** *//**
* Converts a plain vanilla object to be an instance of the class
* passed as the second variable. This is not a recursive funtion
* and will only work for the first level of nesting. When you have
* deeply nested objects, you first need to convert the nested
* objects to class instances, and then convert the top level object.
*
* TODO: This method can be improved by making it recursive. This would be
* done by looking at the typeInfo returned from describeType and determining
* which properties represent custom classes. Those classes would then
* be registerClassAlias'd using getDefinititonByName to get a reference,
* and then objectToInstance would be called on those properties to complete
* the recursive algorithm.
*
* @param object The plain object that should be converted
* @param clazz The type to convert the object to
*/
public static function objectToInstance( object:Object, clazz:Class ):*
...{
var bytes:ByteArray = new ByteArray();
bytes.objectEncoding = ObjectEncoding.AMF0;
// Find the objects and byetArray.writeObject them, adding in the
// class configuration variable name -- essentially, we're constructing
// and AMF packet here that contains the class information so that
// we can simplly byteArray.readObject the sucker for the translation
// Write out the bytes of the original object
var objBytes:ByteArray = new ByteArray();
objBytes.objectEncoding = ObjectEncoding.AMF0;
objBytes.writeObject( object );
// Register all of the classes so they can be decoded via AMF
var typeInfo:XML = describeType( clazz );
var fullyQualifiedName:String = typeInfo.@name.toString().replace( /::/, "." );
registerClassAlias( fullyQualifiedName, clazz );
// Write the new object information starting with the class information
var len:int = fullyQualifiedName.length;
bytes.writeByte( 0x10 ); // 0x10 is AMF0 for "typed object (class instance)"
bytes.writeUTF( fullyQualifiedName );
// After the class name is set up, write the rest of the object
bytes.writeBytes( objBytes, 1 );
// Read in the object with the class property added and return that
bytes.position = 0;
// This generates some ReferenceErrors of the object being passed in
// has properties that aren't in the class instance, and generates TypeErrors
// when property values cannot be converted to correct values (such as false
// being the value, when it needs to be a Date instead). However, these
// errors are not thrown at runtime (and only appear in trace ouput when
// debugging), so a try/catch block isn't necessary. I'm not sure if this
// classifies as a bug or not... but I wanted to explain why if you debug
// you might seem some TypeError or ReferenceError items appear.
var result:* = bytes.readObject();
return result;
}
} // end class
} // end package
...{
import flash.net.ObjectEncoding;
import flash.net.registerClassAlias;
import flash.utils.ByteArray;
import flash.utils.describeType;
import flash.utils.getDefinitionByName;
/** *//**
* Utility class to convert vanilla objects to class instances.
*/
public final class ObjectTranslator
...{
/** *//**
* Converts a plain vanilla object to be an instance of the class
* passed as the second variable. This is not a recursive funtion
* and will only work for the first level of nesting. When you have
* deeply nested objects, you first need to convert the nested
* objects to class instances, and then convert the top level object.
*
* TODO: This method can be improved by making it recursive. This would be
* done by looking at the typeInfo returned from describeType and determining
* which properties represent custom classes. Those classes would then
* be registerClassAlias'd using getDefinititonByName to get a reference,
* and then objectToInstance would be called on those properties to complete
* the recursive algorithm.
*
* @param object The plain object that should be converted
* @param clazz The type to convert the object to
*/
public static function objectToInstance( object:Object, clazz:Class ):*
...{
var bytes:ByteArray = new ByteArray();
bytes.objectEncoding = ObjectEncoding.AMF0;
// Find the objects and byetArray.writeObject them, adding in the
// class configuration variable name -- essentially, we're constructing
// and AMF packet here that contains the class information so that
// we can simplly byteArray.readObject the sucker for the translation
// Write out the bytes of the original object
var objBytes:ByteArray = new ByteArray();
objBytes.objectEncoding = ObjectEncoding.AMF0;
objBytes.writeObject( object );
// Register all of the classes so they can be decoded via AMF
var typeInfo:XML = describeType( clazz );
var fullyQualifiedName:String = typeInfo.@name.toString().replace( /::/, "." );
registerClassAlias( fullyQualifiedName, clazz );
// Write the new object information starting with the class information
var len:int = fullyQualifiedName.length;
bytes.writeByte( 0x10 ); // 0x10 is AMF0 for "typed object (class instance)"
bytes.writeUTF( fullyQualifiedName );
// After the class name is set up, write the rest of the object
bytes.writeBytes( objBytes, 1 );
// Read in the object with the class property added and return that
bytes.position = 0;
// This generates some ReferenceErrors of the object being passed in
// has properties that aren't in the class instance, and generates TypeErrors
// when property values cannot be converted to correct values (such as false
// being the value, when it needs to be a Date instead). However, these
// errors are not thrown at runtime (and only appear in trace ouput when
// debugging), so a try/catch block isn't necessary. I'm not sure if this
// classifies as a bug or not... but I wanted to explain why if you debug
// you might seem some TypeError or ReferenceError items appear.
var result:* = bytes.readObject();
return result;
}
} // end class
} // end package
- AS3 类型强转类
- AS3 强制类型转换
- AS3 强制类型转换
- flash as3 类型转换
- AS3中的八种基元类型
- AS3中的类型转换
- AS3类型转换
- as3,类型默认值
- AS3中的强制类型转换
- AS3.0的类型转换
- AS3全新的变量类型Vector
- AS3 学习笔记 一 变量、类型
- AS3 中string转xml类型
- As3 读取long类型的数字
- AS3.0中的事件类型分类
- AS3 值类型和引用类型的区别
- AS3
- As3
- Windows下程序向Linux下移植细节
- 祝愿我所有的朋友身体健康,事业有成,天天快乐,幸福永远.....
- altas(ajax)控件(七):CollapsiblePanel可折叠面板
- c#中浮点变量的赋值
- ORACLE学习笔记之用户、权限和角色
- AS3 类型强转类
- 图片查看
- asp的access管理程序
- 日期下拉菜单
- 关于创业资金来源
- 端口复用技术与实现代码
- 小鱼写得一篇专利,不知道已经申请生成得专利是否可以在网上发表,不敢贴出来呀,先空个位置吧
- 多线程编程入门介绍
- 适用于各种嵌入式WebServer的数据交互模块可以让页面和WebServer的代码完全脱离耦合