Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 614639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:08:00+00:00 2026-05-13T18:08:00+00:00

Using org.as3commons.reflect I can look-up the class name, and instantiate a class at runtime.

  • 0

Using org.as3commons.reflect I can look-up the class name, and instantiate a class at runtime. I also have (non-working) code which invokes a method. However, I really want to set a property value. I’m not sure if properties are realized as methods internally in Flex.

I have a Metadata class which stores 3 pieces of information: name, value, and type (all are strings). I want to be able to loop through an Array of Metadata objects and set the corresponding properties on the instantiated class.


package com.acme.reporting.builders
{
 import com.acme.reporting.model.Metadata;

 import mx.core.UIComponent;

 import org.as3commons.reflect.ClassUtils;
 import org.as3commons.reflect.MethodInvoker;

 public class UIComponentBuilder implements IUIComponentBuilder
 {
  public function build(metadata:Array):UIComponent
  {
   var typeClass:Class = ClassUtils.forName(getTypeName(metadata));
   var result:* = ClassUtils.newInstance(typeClass);

   for each (var m:Metadata in metadata)
   {
    if (m.name == "type")
     continue;

    // Attempting to invoke as method,
                // would really like the property though

    var methodInvoker:MethodInvoker = new MethodInvoker();

    methodInvoker.target = result;
    methodInvoker.method = m.name;
    methodInvoker.arguments = [m.value];

    var returnValue:* = methodInvoker.invoke(); // Fails!
   }

   return result;
  }

  private static function getTypeName(metadata:Array):String
  {
   if (metadata == null || metadata.length == 0)
    throw new ArgumentError("metadata is null or empty");

   var typeName:String;

   // Type is usually the first entry
   if (metadata.length > 1 && metadata[0] != null && metadata[0].name == "type")
   {
    typeName = metadata[0].value;
   }
   else
   {
    var typeMetadata:Array = metadata.filter(
     function(element:*, index:int, arr:Array):Boolean
     {
      return element.name == "type";
     }
    );

    if (typeMetadata == null || typeMetadata.length != 1)
     throw new ArgumentError("type entry not found in metadata");

    typeName = typeMetadata[0].value;
   }

   if (typeName == null || typeName.length == 0)
    throw new Error("typeName is null or blank");

   return typeName;
  }
 }
}

Here’s some usage code:


var metadata:Array = new Array();

metadata[0] = new Metadata("type", "mx.controls.Text", null);
metadata[1] = new Metadata("text", "Hello World!", null);
metadata[2] = new Metadata("x", "77", null);
metadata[3] = new Metadata("y", "593", null);

this.addChild(new UIComponentBuilder().build(metadata));

I realize that I have to declare a dummy variable of the type I was to instantiate, or use the -inculde compiler directive. An unfortunate drawback of Flex.

Also, right now there’s code to account for typecasting the value to it’s specified type.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T18:08:00+00:00Added an answer on May 13, 2026 at 6:08 pm

    Dynamic execution in AS3 is much simpler than in other languages. This code:

    var methodInvoker:MethodInvoker = new MethodInvoker();
    
    methodInvoker.target = result;
    methodInvoker.method = m.name;
    methodInvoker.arguments = [m.value];
    
    var returnValue:* = methodInvoker.invoke(); // Fails!
    

    can be simplified to this:

    var returnValue:* = result[method](m.value);
    

    EDIT:

    Since it’s a property, it would be done like this:

    result[method] = m.value;
    

    and there is no return value (well, you can call the getter again but it should just return m.value unless the setter/getter do something funky.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using codepad.org and ideone.com for sharing my php code with others and
I just wanna know using org.eclipse.jdt.core.dom.ASTParser if it is possible to parse only a
What is the big difference between launching Equinox via java -jar plugins/org.eclipse.osgi_3.6.0.v20100517.jar and using
Currently, when I contribute property pages to Eclipse using the org.eclipse.ui.propertyPages extension point, the
I am working on a (Codehaus) Maven 2 Mojo. Unfortunately the logic called has
I'm looking to port my working Android XML parser to Blackberry, but the latter's
I am just getting started learning rails. I am building my first app using
I've been trying to transform my XML documents to PDF through Apache FOP, but
Ok a short question: Is there any SIMPLE software with a GUI, that lets
I am getting a JSONException complaining about a very deep nesting (more than 30).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.