supersnake

c#.net

导航

Supersnake.Tools.Tester

Posted on 2005-04-12 09:49  supersnake  阅读(526)  评论(0编辑  收藏  举报
using System;
using System.Reflection;

/**
 * 测试类各种方法
 
*/
  

namespace Supersnake.Tools
{
    
/// <summary>
    
/// Tester 的摘要说明。
    
/// </summary>

    public class Tester
    
{
        
public Tester()
        
{
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
        }


        
/// <summary>
        
/// RunMethod
        
/// </summary>
        
/// <param name="type"></param>
        
/// <param name="sMethod">方法名</param>
        
/// <param name="objInstance">实例</param>
        
/// <param name="objParams">参数</param>
        
/// <param name="flags">绑定标志</param>
        
/// <returns></returns>

        private static object runMethod(System.Type type, 
            
string sMethod,    object objInstance, 
            
object[] objParams,    BindingFlags flags)
        
{
            MethodInfo method;
            
            
try
            
{
                method 
= type.GetMethod(sMethod,flags);
                
if(method==null)
                
{
                    
string msg = "there is no method for type " + type + ".";
                    
throw new ArgumentException(msg);
                }


                
return method.Invoke(objInstance,objParams);
            }

            
catch
            
{
                
throw;
            }

        }
//~RunMethod

        
/// <summary>
        
/// RunInstanceMethod 运行实例方法
        
/// 引发异常: ArgumentException, Exception from Type.GetMethod()
        
/// </summary>
        
/// <param name="type">类型</param>
        
/// <param name="sMethod">方法名</param>
        
/// <param name="objInstance">实例</param>
        
/// <param name="objParams">参数</param>
        
/// <returns>函数返回值</returns>

        public static object RunInstanceMethod(System.Type type, 
            
string sMethod, object objInstance, object[] objParams)
        
{
            BindingFlags flags 
= BindingFlags.Instance 
                
| BindingFlags.Public 
                
| BindingFlags.NonPublic;
            
return runMethod(type,sMethod,objInstance,objParams,flags);
        }


        
/// <summary>
        
/// RunStaticMethod 运行静态方法
        
/// </summary>
        
/// <param name="type">类型</param>
        
/// <param name="sMethod">方法名</param>
        
/// <param name="objParams">参数</param>
        
/// <returns>函数返回值</returns>

        public static object RunStaticMethod(System.Type type,
            
string sMethod,object[] objParams)
        
{
            BindingFlags flags 
= BindingFlags.Static
                
| BindingFlags.Public
                
| BindingFlags.NonPublic;
            
return runMethod(type,sMethod,null,objParams,flags);
        }

    }

}