Scenario:
I have my function names and there parameters stored in the database. The function name along with the parameters are return from the database as
"FunctionName(Convert.ToString("harry"),Convert.ToString("Password"),Convert.ToInt32("5"),Convert.ToString(""),Convert.ToString("AMER_02772"),Convert.ToInt32("0"))"
Question:
Now I want to execute this function returned to me as a string? Please guide me the way to execute this string?
I have read similar sort of post but could not find the exact result.
You can use GetMethod
, this is example code
using System;
using System.Reflection;
static class Methods
{
public static void Inform(string parameter)
{
Console.WriteLine("Inform:parameter={0}", parameter);
}
}
class Program
{
static void Main()
{
// Name of the method we want to call.
string name = "Inform";
// Call it with each of these parameters.
string[] parameters = { "Sam", "Perls" };
// Get MethodInfo.
Type type = typeof(Methods);
MethodInfo info = type.GetMethod(name);
// Loop over parameters.
foreach (string parameter in parameters)
{
info.Invoke(null, new object[] { parameter });
}
}
}
Output
Inform:parameter=Sam
Inform:parameter=Perls
Or
You can reference Dynamically invoking any function by passing function name as string !
And the CodeProject
link Dynamically Invoke A Method, Given Strings with Method Name and Class Name !