Maybe this is commonplace, but working on a class that serializes other classes, I've found this:
I'm working on the following class:
class net.designnation.tests.Serializable
{
private var prop1: String;
private var prop2: Number;
private var prop3: Object;
private var unseen: String;
public static var CONSTANT: String = "This is a constant";
function Serializable( )
{
this.unseen = "hello, world!";
}
public function set val1( param: String )
{
this.prop1 = param + " <:::::::: setter ";
}
public function get val1( ): String
{
return this.prop1 + " <:::::::: getter ";;
}
public function set val2( param: Number )
{
this.prop2 = param + 100;
}
public function get val2( ): Number
{
return this.prop2 + 10000;
}
public function set val3( param: Object )
{
this.prop3 = param;
}
public function get val3( ): Object
{
return this.prop3;
}
}
So, to read and write its properties, we can do the following:
import net.designnation.tests.*
import net.designnation.utils.*
var myClass: Serializable = new Serializable( );
myClass.val1 = "Hi!";
myClass.val2 = 32;
myClass.val3 = { prop: "value" };
trace(" ------------------------\n ");
for ( var k in myClass )
{
trace( k + " == " + myClass[ k ] );
}
//setting
for ( var k in myClass )
{
if ( k == "prop1" ) myClass[ k ] = "modified";
if ( k == "unseen" ) myClass[ k ] = "unseen, but modified";
}
//checking
trace(" \n------------------------\n ");
for ( var k in myClass )
{
trace( k + " == " + myClass[ k ] );
}
But, what if we want to access to those properties through the get/set functions?. We can't do it, because they are not showed in the for .. in loop.
So, using ASSetPropFlags, we can show all the elements of myClass.__proto__, which is something extremely dangerous, because it's an undocumented method, and we don't know if it will be removed or not in the future. Now, all the getters, setters, and even the class constructor will be available.
class net.designnation.utils.showProps
{
static function showAll( obj )
{
_global.ASSetPropFlags( obj.__proto__, null, 0, 1 );
return obj;
}
}
And, now, back to our script:
// Showing all the properties
myClass = showProps.showAll( myClass );
trace(" \n------------------------\n ");
for (var k in myClass)
{
trace( k );
}
//Getting values through the getter methods
trace( " \n------------------------\n " );
trace( " Calling getter methods \n" );
for (var k in myClass)
{
var prefix: String = k.substring( 0, 7 );
var posfix: String = k.substring( 7, k.length);
if ( prefix == "__get__" )
{
trace( myClass[ k ]( ) );
}
}
trace( " \n------------------------\n " );
trace( " setting values using setters \n" );
for (var k in myClass)
{
var prefix: String = k.substring( 0, 7 );
var posfix: String = k.substring( 7, k.length);
if ( prefix == "__set__" )
{
if ( posfix == "val1" ) myClass[ k ]( " the end " );
}
}
trace( myClass.val1 );
This is not a very good practice, but can do the trick. At least it has made it for me.