1 Reply Latest reply on Apr 18, 2009 5:50 AM by wvxvw

    Java-style dynamic class construction

    RichOnYeti

      Hi,

       

      I'd like to be able to instantiate new class instances by name (String) similar to Java's Class.forName()  method.  I've found some references to how this can be accomplished in Flex, but they all fall a bit short of what I need.  They all seem to require a priori knowledge of the classes to be instantiated.  I'm attempting to create a framework that cannot have that kind of knowledge.  Is there a way to accomplish this that I haven't run across, or perhaps this just isn't possible in Flex?  Here's a snippet of what I'd like to do.

       

      private function getClassByName(className:String):SomeInterface
      {
         var classRef:Class = ApplicationDomain.currentDomain.getDefinition(className) as Class;
         var myNewClass:
      SomeInterface = new classRef() as SomeInterface
         return myNewClass;

      }

       

      This code fails with a 1605 exception unless I explicitly refer to the class named by className prior to executing the above code.  Again, this requires a priori knowledge of all classes that can be referred to by className be hard-coded.

       

      Thanks,

      Rich