I'm trying to develop a method to dynamically dispatch a method from my C# .NET application to a database stored procedure. I already have a method to call the stored procedure which accepts the procedure name and a Hashtable of parameters. I want to be able to call that method from any other method in my application, so I've built the following (where ParameterMap is a static Hashtable of stored procedure parameter names:
public Foo DoSomething(int x, int y)
{
var mb = MethodBase.GetCurrentMethod();
var parameters = new Hashtable();
foreach (var pi in mb.GetParameters())
{
var storedProcMethodName = (ArrayList)ParameterMap[mb.Name];
var storedProcParameterName = storedProcMethodName[pi.Position];
parameters.Add(storedProcParameterName, ???);
}
return (Foo)
CallDatabaseMemberFunction("GET", parameters, typeof(Foo));
}
I want to replace ??? with the parameter values in x and y, accessing them by position, thereby mapping stored procedure parameter names to this method's parameter values. Any other approach to this problem would be welcome as well.