在Flex3中,对象属性是什么类型?

发布于 2024-08-03 11:55:46 字数 904 浏览 3 评论 0原文

我有很多像这样的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

辞慾 2024-08-10 11:55:46

您可能需要类似 describeType 。如果你想创建一个真实的对象,也许你需要使用 getDefinitionByName() 。所以你的函数的内容是这样的:

var typeXml:XML = describeType(type_var[property]);
return getDefinitionByName(typeXml.type[0].@name);

我还没有编译它。只是把它扔到那里看看是否有帮助。

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:

var typeXml:XML = describeType(type_var[property]);
return getDefinitionByName(typeXml.type[0].@name);

I haven't compiled it. Just throwing it out there to see if it helps.

友谊不毕业 2024-08-10 11:55:46

您可以使用“is”运算符来检查对象的类型。

is 运算

function map(obj, targetType): * {
  var newInstance: * = new targetType();
  for (var property: String in obj) {
    if (obj[property] is Date) {
      newInstance[property] = parseDate(obj[property]);
    }
    else {
      newInstance[property] = obj[property];
    }
  }
}

You can use the 'is' operator to check the type of an object.

The is operator

function map(obj, targetType): * {
  var newInstance: * = new targetType();
  for (var property: String in obj) {
    if (obj[property] is Date) {
      newInstance[property] = parseDate(obj[property]);
    }
    else {
      newInstance[property] = obj[property];
    }
  }
}

hth

Koen

旧伤还要旧人安 2024-08-10 11:55:46

如果您需要将对象变量映射到 MyData 变量类,您可以执行以下

public class MyData 
{
    public var time: Date;
    public var label: String;

    function map(obj:Object):void
    {
      for (var property: String in obj) 
      {
          this[property] = obj[property];
      }
    }
}

操作 注意:对象 obj 必须包含确切的“时间”和“标签”属性。

希望它能解决您的问题

If you need to map an Object variable to a variable class as MyData you can do the following

public class MyData 
{
    public var time: Date;
    public var label: String;

    function map(obj:Object):void
    {
      for (var property: String in obj) 
      {
          this[property] = obj[property];
      }
    }
}

Note: The object obj must contain the exact "time" and "label" properties.

Hope it solves your problem

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文