|
using System;
using System.Reflection;
namespace UnitTestUtilities
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Helper
{
private Helper()
{
}
#region Run Method
/// <summary>
/// Runs a method on a type, given its parameters. This is useful for
/// calling private methods.
/// </summary>
/// <param name="t"></param>
/// <param name="strMethod"></param>
/// <param name="aobjParams"></param>
/// <returns>The return value of the called method.</returns>
public static object RunStaticMethod(System.Type t, string strMethod, object [] aobjParams)
{
BindingFlags eFlags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
return RunMethod(t, strMethod, null, aobjParams, eFlags);
} //end of method
public static object RunInstanceMethod(System.Type t, string strMethod, object objInstance, object [] aobjParams)
{
BindingFlags eFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
return RunMethod(t, strMethod, objInstance, aobjParams, eFlags);
} //end of method
private static object RunMethod(System.Type t, string strMethod, object objInstance, object [] aobjParams, BindingFlags eFlags)
{
MethodInfo m;
try
{
m = t.GetMethod(strMethod, eFlags);
if (m == null)
{
throw new ArgumentException("There is no method '" + strMethod + "' for type '" + t.ToString() + "'.");
}
object objRet = m.Invoke(objInstance, aobjParams);
return objRet;
}
catch
{
throw;
}
} //end of method
#endregion
} //end of class
} //end of namespace
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Tim Stall is a Chicago-based technical consultant for Computer Sciences Corporation (www.csc.com), a leading global IT services company. In addition to his expertise in Microsoft.Net development projects and enterprise architecture, Tim's .Net experience includes, writing technical publications, leading internal training, and having MCAD certification. His blog is at http://timstall.dotnetdevelopersjournal.com/.