views:

43

answers:

2

it may be a duplicate of

http://stackoverflow.com/questions/1120228/how-to-dynamically-call-a-class-method-in-net

and of

http://stackoverflow.com/questions/3918431/how-to-achieve-calling-a-function-dynamically-thats-right-the-function-to-be-cal

but the above two have solutions which as the answers said are complicated, not for a beginner i guess.

and

both solutions contain "type" which from the code i think is for defining the class the method belongs to.

like

static void caller(String myclass, String mymethod)
    {
        // Get a type from the string 
        Type type = Type.GetType(myclass);
        // Create an instance of that type
        Object obj = Activator.CreateInstance(type);
        // Retrieve the method you are looking for
        MethodInfo methodInfo = type.GetMethod(mymethod);
        // Invoke the method on the instance we created above
        methodInfo.Invoke(obj, null);
    }

but my initial web site, only contains one class common to all the functions,

a database which has "function name" "func id"

supposed :- function name exactly same as that in code

i only want to achieve the following

  • get the string value of function name according to the id mentioned in the text box

  • now call that function, whose name is in the string variable

problem

the methodinfo, needs the "type.GetMethod(mymethod);"

..

+2  A: 

In order to call a function you need to specify the type this function is declared on. If all functions you are going to call are declared on a common class you could do the following:

static void CallFunc(string mymethod)
{
    // Get a type from the string 
    Type type = typeof(TypeThatContainsCommonFunctions);

    // Create an instance of that type
    object obj = Activator.CreateInstance(type);

    // Retrieve the method you are looking for
    MethodInfo methodInfo = type.GetMethod(mymethod);

    // Invoke the method on the instance we created above
    methodInfo.Invoke(obj, null);
}

If the functions you are going to call are static you don't need an instance of the type:

static void CallFunc(string mymethod)
{
    // Get a type from the string 
    Type type = typeof(TypeThatContainsCommonFunctions);

    // Retrieve the method you are looking for
    MethodInfo methodInfo = type.GetMethod(mymethod);

    // Invoke the method on the type
    methodInfo.Invoke(null, null);
}
Darin Dimitrov
with possibly some null checking! ;)
Mitch Wheat
what do you mean by functions are static ?, how can a function be dynamic? please explain this point, will be great ful thanks
static and instance methods - http://msdn.microsoft.com/en-us/library/aa645766(VS.71).aspx
Darin Dimitrov
please help at http://stackoverflow.com/questions/3976211/used-reflection-in-c-works-but-two-errors-need-help-solving-function-called-ju
+1  A: 

I see 2 solutions:

  1. you need to map function id to real function name
  2. call type.GetMethods() to get list of all methods and choose right one
Arseny
http://stackoverflow.com/questions/3976211/used-reflection-in-c-works-but-two-errors-need-help-solving-function-called-ju please help