请教如何反射一个可变参数的方法?
例如:
public static class cls1{
public static void fncInvoke(params string[] sAry){
// do something
}
}
请教我如何来反射这个 cls1.fncInvoke 静态方法?
例如:
public static class cls1{
public static void fncInvoke(params string[] sAry){
// do something
}
}
请教我如何来反射这个 cls1.fncInvoke 静态方法?
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;
}
return mthInfo.Invoke( null, new object[] { new string[] { "Abc,s","," } } );
地方抱错了。