在Flex中使用Introspection调用静态方法

时间:2010-09-30 18:35:45

标签: flex static introspection method-invocation

所有

虽然这与另一个post类似,但该帖子(并未说明如何执行此操作(如果可以),而不实例化对象。此外,如果没有成功,我尝试了多个主题的变体:

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

我是Flex新手,但在C#和Java中都使用了Reflection。顺便说一下,我试图在Flex中工作的代码在这两种语言中都有镜像,并按预期工作。

感谢任何帮助, 谢谢, 托德

无效的Flex代码:

    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代码:

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#代码:

    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 个答案:

答案 0 :(得分:1)

这对我有用:

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

这也有效:

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

注意:'call'方法的第一个参数(本例中为MyClass)只是指定在函数内部使用'this'关键字时引用的对象。