在Flex3中,对象属性是什么类型?
我有很多像这样的 Flex 对象:
public class MyData {
public var time: Date;
public var label: String;
}
我从通过 AMF 检索的数据库记录填充该对象,看起来像这样:
{
label: "Label",
incident: "2009-08-15 11:12:14.12233"
}
我想为这些对象编写一个通用值映射器,给定一个目标对象(的实例) MyData
此处)和输入记录,将能够知道 MyData.time
是一个 Date
字段并自动执行类型映射。像这样:
function map(obj, targetType): * {
var newInstance: * = new targetType();
for (var property: String in obj) {
if (getPropertyType(targetType, property) == Date) {
newInstance[property] = parseDate(obj[property]);
}
else {
newInstance[property] = obj[property];
}
}
}
function getPropertyType(type_var: Class, property: String): Class {
// .. this is what I have no idea how to do
}
有人可以填这里的空白吗?
I have many Flex objects like this one:
public class MyData {
public var time: Date;
public var label: String;
}
I am populating this object from a DB record retrieved via AMF that looks something like this:
{
label: "Label",
incident: "2009-08-15 11:12:14.12233"
}
I want to write a generic value mapper for these object that, given a target object (instance of MyData
here) and an input record, will be able to tell that MyData.time
is a Date
field and perform type mapping automatically. Something like this:
function map(obj, targetType): * {
var newInstance: * = new targetType();
for (var property: String in obj) {
if (getPropertyType(targetType, property) == Date) {
newInstance[property] = parseDate(obj[property]);
}
else {
newInstance[property] = obj[property];
}
}
}
function getPropertyType(type_var: Class, property: String): Class {
// .. this is what I have no idea how to do
}
Can someone fill in the blank here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能需要类似 describeType 。如果你想创建一个真实的对象,也许你需要使用 getDefinitionByName() 。所以你的函数的内容是这样的:
我还没有编译它。只是把它扔到那里看看是否有帮助。
You possibly need something like describeType. And maybe you need to use getDefinitionByName() if you want to make to a real object. So something like this for the contents of your function:
I haven't compiled it. Just throwing it out there to see if it helps.
您可以使用“is”运算符来检查对象的类型。
is 运算
符
You can use the 'is' operator to check the type of an object.
The is operator
hth
Koen
如果您需要将对象变量映射到 MyData 变量类,您可以执行以下
操作 注意:对象 obj 必须包含确切的“时间”和“标签”属性。
希望它能解决您的问题
If you need to map an Object variable to a variable class as MyData you can do the following
Note: The object obj must contain the exact "time" and "label" properties.
Hope it solves your problem