开发者

Invoke static method using Introspection in Flex

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");
        }
    }


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜