A few days ago one of the other guys here on the blog talked about
getting object information in
C#,
well today I am going to talk about doing the same basic task in Flex
and Actionscript. This tutorial is going to take a look at two methods
that will help get object information, describeType() and
ObjectUtil.getClassInfo(). Both the aforementioned functions will
return information about the object passed, they differ in how much
information they return and how.
Below is a small example showing the output for each of the functions
for a very simple class (described later). You will notice that
describeType returns the information as XML and includes public
properties (accessors), variables, and methods. The second piece of
output is produced by calling ObjectUtil.toString on the result
returned by ObjectUtil.getClassInfo which is a generic object. Well
take a second to look over the output. You can also look at the class
information example source
code.
Well the first thing we should take a gander at is the class we are going to be getting information from. In this case it is a simple class I created that wouldn't really be used for anything. So here it is.
package
{
import mx.core.IDataRenderer;
public class MyFunClass implements IDataRenderer
{
public var funActivity:String;
private var _age:int = 22;
private var _favoriteNumber:int = 42;
public function get favoriteNumber():int
{
return this._favoriteNumber;
}
public function set favoriteNumber(value:int):void
{
this._favoriteNumber = value;
}
public function sayMyName(name:String):String
{
return name + " is the Greatest!";
}
public function sayMyAge():int
{
return this._age;
}
//Implementing IDataRenderer
public function get data():Object
{
return {};
}
public function set data(value:Object):void
{
var o:Object = value;
}
}
}
As you can see from above we have a class extending nothing, so it
extends Object, and implementing one Interface. We have one public
variable, a couple public properties and a couple public functions. The
first method of getting this information is going to be using the
function describeType which returns an
XML
object. Now this means we could use
e4x
to get to the information found in the returned XML. This function is
also in the namespace flash.utils which means it is also available
when writing Actionscript code that doesn't have the Flex libraries. The
following code demonstrates using the function.
var mc:MyFunClass = new MyFunClass();
var dt:XML = describeType(mc);
trace(dt.toXMLString());
Taking a look at the documentation for the second function,
ObjectUtil.getClassInfo, we can see that this function returns class
information and the public properties. This function will not, however,
return the public functions available on the object. This is one of the
limitations of using this function - another is that it is found in the
Flex library. This means to use the function you need to be using the
Flex framework. It has the advantage, in my opinion, of returning the
results as an Object. This function is just as easy to use as the first
one.
var mc:MyFunClass = new MyFunClass();
var ci:Object = ObjectUtil.getClassInfo(mc);
trace(ObjectUtil.toString(ci));
With that chunk of code we have gone over two ways of getting class
information in Flex and Actionscript. I will say that in most situations
I probably use describeType to get the full information, this would
also be good to use for serialization. ObjectUtil.getClassInfo has its
uses though, so don't forget about it. Well if anyone has a question
feel free to drop us a comment. Until next time don't forget to eat your
veggies and keep coding.
Source Files:
Hi. I understand the codeā¦but from a practical perspective when and why would you need to get class information?
Cheers.
I have seen this used to check the list of implemented interfaces of an object before. Another reason would be to implement custom metadata.
This is interesting but I'm curious why would you want to get the data by xml or formatted as a String when you already have access to those properties via the object. So I'm not too sure why you want to access them by another format.
These are the only ways (that I know) to get a list of interfaces implemented or a list of metadata on an object. Even getting all the properties on a typed object is hard. Mostly this stuff would be used to handle runtime object in which you may not know what is actually on the object.
can some explain to me how to get a specific object out of a arraycollection.
say if i when i start my desktop program. I want the username and userid to be populated into the textinput fields of that item. How could I accomplish this.
Thanks ahead of time.