C#, Calling a procedure with a paramter (Replacing case statement)

Posted on 2007-07-31
Last Modified: 2008-01-09
I am currently calling a procedure based on a paramter from a database table where I am having to use a case statement.   In this manner I am having hard code my procedures with a case statement.  Please refer to the example:

CreateMe(string ProcedureName)
  case "FirstProc":
    case "SecondProc":

I am wanting to be ablle to perform the same process but without having to hardcode (case statement) so that it can then be driven by the database.

Any ideas please....

Question by:vandy02
    LVL 29

    Expert Comment

    by:Gautham Janardhan
    can u explain a bit more depending on the ProcedureName u call different functions  FirstProc();
     etc rt ?
    and u want to generalize it
    LVL 4

    Accepted Solution

    You can do it with Reflection. Take a read in this article:

    Regards, Rodrigo Leote
    LVL 96

    Assisted Solution

    by:Bob Learned
    Here is a clean and simple example:

    using System;
    using System.Reflection;

    public class Reflect

        public static object ExecuteMethod(string name, params object[] parameters)
            Assembly assy = Assembly.GetExecutingAssembly();
            string ns = assy.GetType().Namespace;

            MethodInfo method = assy.GetType().GetMethod(ns + "." + name);
            return method.Invoke(null, parameters);


    LVL 12

    Assisted Solution

    ..or have a lookup dictionary...

    delegate void TargetFn();

    Dictionary<string,TargetFn> lookup;

    // populate once at startup
    lookup = new Dictionary...
    lookup["FirstProc"] = FirstProc;

    Then, call via:


    Magic :-)

    (Give or take tests to check the procedureName is valid etc. ANd you can then change your key to be whatever you like - doesn't need to be the procedure name.)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    This video discusses moving either the default database or any database to a new volume.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now