【问题标题】:Mirroring a method with string parameter使用字符串参数镜像方法
【发布时间】:2012-05-11 22:36:55
【问题描述】:

我正在尝试使用镜像来调用 MyClass 类的方法 s1,参数为 s2。 Java 抱怨 String.TYPE 不存在。我检查了 API,它是正确的:我不能做与调用 Integer.TYPE 时相同的事情。但是我该如何解决这个问题呢?我需要 String 类型的 partype,否则方法会抛出异常。

public void trying(MyClass method, String s1, String s2){
   try {
       Class cls = Class.forName("MyClass");
       Class partype[] = new Class[1];
       partype[0] = String.TYPE;
       Method meth = cls.getMethod(s1, partype);
       meth.invoke(methobj, s2);
   }
   catch (Throwable e) {
        System.err.println(e);
   }
}

【问题讨论】:

    标签: java string reflection mirroring


    【解决方案1】:

    不是类型,是class

    partype[0] = String.class;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-09
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 2016-10-14
      相关资源
      最近更新 更多