Flex and Actionscript Getting Class Information
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 |
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.
{
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 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 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.
Posted in Flex, All Tutorials by The Fattest |
