Extending dynamically loaded classes in AS2

There was a question from Iiley on the OSFlash Mailing List today, asking whether it is possible to extend classes that are dynamically loaded into Flash applications as per my SWF as DLL tutorial on OSFlash. It is, indeed, possible: Mark your library classes as "dynamic intrinsic" and then use the good old AS1-style prototype object to extend the class once it has been loaded by your main application.

In other words:

ActionScript:
  1. // You must define intrinsic classes if you want type checking to work with dynamically loaded classes
  2. // If you make it *dynamic* also, you can extend it after loading it into your app.
  3.  
  4. dynamic intrinsic class com.ariaware.tests.dll.library.FirstClass
  5. {
  6. public function aMethod():Void;
  7. }

And, in your main application:

ActionScript:
  1. // Called when the DLL has completely loaded
  2. private function dllComplete ( evt:DLLEvent )
  3. {
  4. //...
  5.  
  6. //
  7. // Extend the library classes
  8. //
  9. FirstClass.prototype.aNewMethod = function ()
  10. {
  11. trace ("AS1 style baby!");
  12. };
  13. //
  14. // Test the library classes
  15. //
  16. var firstClass:FirstClass = new FirstClass();
  17. firstClass.aMethod();
  18. firstClass.aNewMethod();
  19.  
  20. //...
  21. }

(I've only shown the changes from the SWF as DLL example in the previous post.)

You can download the new example files here (~25K).