views:

46

answers:

1

All,

While this is similar to another post, that post (does not indicate how to perform this (if it can be done) without instantiating an object. Also, without success I have tried multiple variations on the theme of:

class[method](arg)
class[method].call(arg)
method.apply(class, arg)

I am new to Flex, but have used Reflection in both C# and Java. BTW, the code that I am attempting to get to work in Flex is mirrored in both those languages and works as expected.

Any help is appreciated, Thanks, Todd

Non-functioning Flex Code:

    private function ipMethodTester( ipMethodName:String,
             shouldPass:Array, shouldFail:Array):void
    {
        var message:String;
        var entry:String;
        for each(entry in shouldPass)
        {
            message = ipMethodName + ": " + entry + " should pass";
            try
            {
                Assert.assertTrue(message,
                    FieldValidator[ipMethodName](entry));
            }
            catch(e:Error)
            {
               Assert.fail(e.message + " " + message);
            }
        }
        for each(entry in shouldFail)
        {
            message = ipMethodName + ": " + entry + " should fail";
            try
            {
                Assert.assertFalse(message,
                    FieldValidator[ipMethodName](entry));
            }
            catch(e:Error)
            {
               Assert.fail(e.message + " " + message);
            }
        }
    }

Java Code:

private void ipMethodTester(final String ipMethodName,
         final String[] shouldPass, final String[] shouldFail)
{
   Method method;
   try
   {
      method = InetUtil.class.getDeclaredMethod(ipMethodName, String.class);
      method.setAccessible(true);

      for(String entry : shouldPass)
      {
         Object[] invokeArgs = { entry };
         boolean passed = (Boolean)method.invoke(null, invokeArgs);

         assertTrue(ipMethodName + ": " + entry + " should pass", passed);
      }

      for(String entry : shouldFail)
      {
         Object[] invokeArgs = { entry };
         boolean passed = (Boolean)method.invoke(null, invokeArgs);

         assertFalse(ipMethodName + ": " + entry + " should fail", passed);
      }
  }
  catch (final Exception e)
  {
     fail(e.getClass().toString());
  }
}

C# code:

    private void ipMethodTester(string ipMethodName, string[] shouldPass, string[] shouldFail)
    {
        Type type = typeof (ValidateUtil);
        BindingFlags bindingFlags = BindingFlags.InvokeMethod
            | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static;

        MethodInfo method = type.GetMethod(ipMethodName, bindingFlags);

        foreach(string entry in shouldPass)
        {
            object[] invokeArgs = { entry };
            bool passed = (bool)method.Invoke(null, invokeArgs);

            Assert.IsTrue(passed, ipMethodName + ": " + entry + " should pass");
        }

        foreach(string entry in shouldFail)
        {
            object[] invokeArgs = { entry };
            bool passed = (bool)method.Invoke(null, invokeArgs);

            Assert.IsFalse(passed, ipMethodName + ": " + entry + " should fail");
        }
    }
+1  A: 

This works for me:

MyClass['myMethod']('arg1','arg2');

This also works:

MyClass['myMethod'].call(MyClass, 'arg1', 'arg2');

Note: the first argument of the 'call' method (MyClass in this case) just specifies which object is referenced when you use the 'this' keyword inside the function.

Dane
Dane, are the words in ticks considered strings?
Todd
I am getting "Error 1034 Type Coercion failed" with both of those. I have checked and my arg type matches the arg type of the static method. I don't know where else the coercion could be coming from.
Todd
Yes, they are strings. Can you paste in your code?
Dane
Dane, added the non-functioning flex code to the original question.
Todd
Hmm, that looks fine to me. I think the problem must be occurring somewhere else in your code.
Dane
Dane, I need more clarification on the args. Are all values passed required to be strings or are you indicating that myMethod takes string arguments?
Todd
The latter. Arguments can be any type, but in my example I used strings.
Dane
Dane, thanks for all your help and patience. Apparently, my static block initialization is the root of the errors.
Todd