Shamil Salakhetdinov
shamil at smsconsulting.spb.ru
Sat Nov 7 08:19:11 CST 2009
Hi John and all,
Just wanted to post a bit more advanced sample on .NET reflection and how it
can be used to instantiate custom classes and to execute their parameterized
methods based on text definition:
using System;
using System.Reflection;
#region Test Program
namespace NetReflectionSample
{
/// <summary>
/// Test class
/// </summary>
class Program
{
static void Main(string[] args)
{
// List of custom classes (with parameters) to instantiate and
// call their Run(...) method
string[] spRunnersCalls =
{
"NetReflectionSample.SP1",
"NetReflectionSample.SP2|503|Test String|3.14|11/7/2009",
"NetReflectionSample.SP3|123|Test String2"
};
foreach (string spRunnerCall in spRunnersCalls)
{
// Parse current SP call string definition and
// return StoredProcedureRunner instance
StoredProcedureRunner runner =
DynamicMethodCallParser.Parse(spRunnerCall);
runner.Init();
// If there are calling parameters use CustomRunMethodInfo
to
// execute SP call wrapper method, else use default Run()
method
CustomRunMethodInfo customRunner = runner as
CustomRunMethodInfo;
if (customRunner.Parameters != null)
{
customRunner.ParameterizedRunMethodInfo.Invoke(
customRunner,
customRunner.Parameters);
}
else runner.Run();
// collect execution stats
runner.CollectStats();
Console.WriteLine();
}
}
}
}
#endregion
Thank you.
--Shamil