Thanks for your reply.
I did try the code on Sun Java first (I work predominantly with it as
opposed to VJ#) and it does work. The first argument passed to
Method.invoke() is 'null' because the method is a static method and therefore
isn't required. No harm in adding it though.
The second argument is the array of arguments to be used in invoking the
method. It's interesting that passing the Class [] to invoke() works where
passing the Object[] containing the String doesn't. Both java.lang.Class and
java.lang.String implement java.io.Serializable so both should work.
Here's an additional test that further exemplifies the problem:
...
public static boolean test2( java.lang.Comparable c ) {
return true;
}
public static void main( String [] args ) {
try {
boolean b = java.io.Serializable.class.isInstance( String.class );
System.out.println( "b : " + b );
Class [] params = new Class[] { java.lang.Comparable.class };
StringTest st = new StringTest();
Method m = st.getClass().getMethod( "test2", params );
Object [] theArgs = new Object[]{ "Foo" };
Object result = m.invoke( st, theArgs );
System.out.println( result );
}
catch( Exception e ) {
e.printStackTrace();
}
...
Convinced it's a bug yet? :)
Andy Wu
[quoted text, click to view] "Lars-Inge Tønnessen [VJ# MVP]" wrote:
> Hi Andy,
>
> I don't think this is a bug. (What you do is not working in Sun Java. J# and
> Sun Java gives the same result.)
>
> You forget to tell invoke what object you want to do the invoke on. You also
> forget to give the correct arguments to the method.
>
> Please see "//PLEASE SEE HERE" in the example.
>
>
> import java.lang.reflect.*;
> public class StringTest
> {
> public StringTest(){}
>
> public static boolean test( java.io.Serializable s )
> {
> return true;
> }
>
> public static void main( String [] args )
> {
> try
> {
> Class [] params = new Class[] { java.io.Serializable.class };
> StringTest st = new StringTest();
> Method m = st.getClass().getMethod( "test", params );
>
> // Object [] theArgs = new Object[]{ "Foo" };
> //Object result = m.invoke( null, theArgs );
>
> // PLEASE SEE HERE
> Object result = m.invoke( st, params );
> System.out.println( result );
> }
> catch( Exception e )
> {
> e.printStackTrace();
> }
> }
> }
>
>
>
> Regards,
> Lars-Inge Tønnessen
>
>
>