Thứ Sáu, 30 tháng 10, 2009

Dynamic Class

Dynamic Class
private var forCompiler:Person;

private function cc():void

{

        var obj:Object = createInstance("Person", ["bob", 30]);<br>
}

public function createInstance(className:String, args:Array):Object<br>
{<br>

        var myClass:Class = getDefinitionByName(className) as Class;
        var instance:Object = new myClass();
        instance.initArgs.apply(null, args);
        return instance;
}

/*********************************************************************

//In person.initArgs()....

//Note: the constructor also accepts name & age, assigning them default values

//if not specified.

public function initArgs(name:String, age:uint):void

{

        this.name = name;

        this.age = age;

}

***
function listProperties(obj:Object, accessType:String="readwrite", excludeCommonWith:Class=null, matchesRE:RegExp=null, declaredOnlyByObjClass:Boolean=true):Array
{
 var a:XMLList;
 var b:Array=[];
 var c:*;
 var n:int;
 var doc:XML;
 var cname:String;
 matchesRE=matchesRE || new RegExp(".+");
 accessType=accessType || "readwrite";
 accessType=accessType.toLowerCase();
 if (!RegExp(/readwrite|readonly/i).test(accessType)) {
  accessType == "readwrite";
 }
 doc=flash.utils.describeType(obj);
 cname=doc.@name;
 if (declaredOnlyByObjClass) {
  a=doc.accessor.(@access == accessType && @declaredBy == cname && matchesRE["test"](@name));
 } else {
  a=doc.accessor.(@access == accessType && matchesRE["test"](@name));
 }
 if (excludeCommonWith != null) {
  c=new excludeCommonWith();
 }
 n=0;
 while (n < a.length())
 {
  if (c != null) {
   if (Object(c).hasOwnProperty(a[n].@name) == false) {
    b.push(a[n].@name);
   }
  } else {
   b.push(a[n].@name);
  }
  n++;
 }
 return b;
}


http://keith-hair.net/blog/2009/12/08/listing-class-properties-dynamically-in-as3/#more-119

0 nhận xét:

Đăng nhận xét