tags:

views:

43

answers:

1

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.

+1  A: 

You cannot retrieve the parameter values in this way, at least not in .NET.

You will have to write the code out explicitly (I’ve removed the assignment to storedProcMethodName because you’re not using it anywhere):

public Foo DoSomething(int x, int y)
{
    var mb = MethodBase.GetCurrentMethod();
    var parameters = new Hashtable();

    parameters.Add("x", x);
    parameters.Add("y", y);

    return (Foo)
        CallDatabaseMemberFunction("GET", parameters, typeof(Foo));
}

If you need this often, you should consider writing a tool that auto-generates this code. All the SQL layers I know of (IQToolkit, SubSonic, LINQ-to-SQL) do so.

Timwi
I was afraid this might be true. Thank you.
Jason Dufair