3 Replies Latest reply on Sep 27, 2006 7:28 AM by Ed_Syrett

    Reflection in AS3

    /usr/local/ΕΨΗΕΛΩΝ
      Hi,
      I'm getting acquainted with Flex Builder and I think it's a fantastic software. I come from the world of .NET Framework and I just learnt to use Reflection in that framework. Here's my question anyway.

      I want ActionScript3 to behave like the following: instantiate a class from an external file when I don't know both class name and file name at compile time.
      For example, the application loads a text file (runtime generated) with the names of SWC files and classes. It then creates a menu, where each button loads the component that is specified in the file.

      In .NET Framework, Reflection allows me to. I can know the DLL name and the class name as strings, and I can use the System.Reflection.Assembly object to dynamic link to the DLL and load the class. Of course I get exception in case something goes wrong (DLL missing, class not declared, etc.).

      I don't want to use SWFLoader since, as I know, I can't pass parameters to the constructor.

      Is it possible to do something like .NET's reflection in AS3?

      Thank you
        • 1. Re: Reflection in AS3
          Ed_Syrett
          SWFLoader is your best chance here as the SWF is basically the equivalent of the .NET assembly.

          You can pass params to the loaded SWF. Here's a link to livedocs that shows you how:

          Using SWFLoader

          In this example, they load the swf using @embed. For my tests, I just put in the file path (i.e. "C:\\development\\..." etc)

          The other gotcha is that the example has this strange cast "local()" and I wondered for a while what that was. But the class of the loaded app is local.mxml, which implies that you have to know the class name of the loaded app. But if you're loading dynamically without knowing the class name, that's plainly pants. Instead, I derived the loaded app from a base class that is known to both the container app and the loaded app. Then you have a sort of "Facade" for all your loaded components (look for Facade as a Design Pattern on the web). You can then reference all your loaded apps through the Facade class.

          Hope this makes sense - I can actually send you my test app if you want....:-D
          • 2. Re: Reflection in AS3
            /usr/local/ΕΨΗΕΛΩΝ Level 1
            Well... I can't follow you in the second part of your reply. Can you send me you application so I'll be able to see the code?

            I'm now trying to edit the Adobe documentation example to load a SWF file dynamically...
            • 3. Re: Reflection in AS3
              Ed_Syrett Level 1
              I'd be quite happy to send you the app that I created to test this out... but your profile isn't visible so I haven't got your email address. Go into your profile/personal options and set your profile and email to be visible....