请教如何反射一个可变参数的方法?
例如:
public static class cls1{
    public static void fncInvoke(params string[] sAry){
        // do something
    } 
}
请教我如何来反射这个 cls1.fncInvoke 静态方法?

解决方案 »

  1.   

     跟普通方法没什么区别吧?typeof(cls1).GetMethod("fncInvoke",new Type[]{typeof(string[])})
      

  2.   

    不好意思,我可能没有说明白,我想知道用MethordInfo.Invoke(null,object[])的第二个参数怎么设定?
      

  3.   

    MethordInfo.Invoke(null,object[]{new string[]{"a","b"}})
      

  4.   

    private object TestInstance( string clsName, string mthName, params object[] objArg ) {
                Type typClass = null;
                object objClass = null;
                object objReturn = null;
                Assembly[] ablAry = AppDomain.CurrentDomain.GetAssemblies();
                try {
                    foreach ( Assembly abl in ablAry ) {
                        foreach ( Type t in abl.GetTypes() )
                            if ( t.FullName.EndsWith( clsName ) ) {
                                MethodInfo mthInfo = t.GetMethod( mthName );
                                if ( ! IsStaticClass(  typClass = t  ) ) 
                                    objClass = abl.CreateInstance( ( typClass = t ).FullName );
                                int iArg = mthInfo.GetParameters().Length;
                                object[] objSendArg = new object[iArg];
                                if ( objArg != null && objArg.Length > 0 ) {
                                    if ( objArg.Length < iArg )
                                        throw new Exception( "调用参数的个数不够。" );
                                    else
                                        for ( int i = 0; i < iArg; i++ )
                                            objSendArg[ i ] = objArg[ i ];
                                }
                                if ( mthInfo.IsStatic )
                                    //return mthInfo.Invoke( null, ( objArg == null && mthInfo.GetParameters().Length > 0 ? new object[] { null } : new object[] { objSendArg } ) );
                                    return mthInfo.Invoke( null, new object[] { new string[] { "Abc,s","," } } );                            else
                                    objReturn = ( mthInfo.Invoke( objClass, objSendArg ) );
                                throw new Exception( "反射成功。" );
                            }
                    }
                } catch ( Exception ex ) {
                    Console.WriteLine( ex.Message );
                } finally {
                    if ( objClass != null && typClass.GetMethod( "Dispose" ) != null )
                        typClass.GetMethod( "Dispose" ).Invoke( objClass, null );
                }
                return objReturn;
            }
      

  5.   

    MethordInfo.Invoke(null,new object[]{"1","2"})
      

  6.   

    我没有忽悠阿,就在那个
    return mthInfo.Invoke( null, new object[] { new string[] { "Abc,s","," } } );
    地方抱错了。