我有以下类树:publicclassA{publicstaticobjectGetMe(SomeOtherClasssomething){returnsomething.Foo();}}publicclassB:A{publicstaticnewobjectGetMe(SomeOtherClasssomething){returnsomething.Bar();}}publicclassC:B{}publicclassSomeOtherClass{}给定SomeOtherClassparameter=newSomeOtherClass())这有效:typeof(B).GetMethod
我有以下代码:publicvoidmyMethod(Object...args){System.out.println("thisismyMethod");}publicvoidinvokeMyMethod(){Methods=this.getClass().getMethod("myMethod",Object[].class);Object[]ex=newObject[2];ex[0]="hi";ex[1]="there";s.invoke(this,ex);}我收到异常java.lang.IllegalArgumentException:参数数量错误。怎么了?
给定:classA{publicvoidm(Listl){...}}假设我想通过反射调用方法m,将ArrayList作为参数传递给m:ListmyList=newArrayList();Aa=newA();Methodmethod=A.class.getMethod("m",newClass[]{myList.getClass()});method.invoke(a,Object[]{myList});第3行的getMethod将抛出NoSuchMethodException,因为myList的运行时类型是ArrayList,而不是List。是否有不需要了解类A的参数类型的良好通用方法
我正在使用反射来获取TryParse方法信息(为第一个猜出原因的人投票;)。如果我调用:typeof(Int32).GetMethod("Parse",BindingFlags.Static|BindingFlags.Public,null,newType[]{typeof(string)},null);我得到了一个方法,但稍微扩展了这个:typeof(Int32).GetMethod("TryParse",BindingFlags.Static|BindingFlags.Public,null,newType[]{typeof(string),typeof(Int32)},null)
这个问题在这里已经有了答案:GetagenericmethodwithoutusingGetMethods(10个答案)关闭9年前。我正在尝试检索可枚举类型的Where方法的MethodInfo:typeof(Enumerable).GetMethod("Where",newType[]{typeof(IEnumerable),typeof(Func)})但得到空值。我做错了什么?
这是类(class):classFoo{publicvoidbar(inta,Objectb){}}现在我正试图从类中“反射(reflect)”这个方法:Classc=Foo.class;Class[]types={...};//whatshouldbehere?Methodm=c.getMethod("bar",types); 最佳答案 只有一个int.class。Class[]types={int.class,Object.class};另一种选择是Integer.TYPE.Class[]types={Integer.TYPE,