0

I have an interesting question today. I have a table that stores a void name and from what class file it came from.

Is it possible to read the table and assign a var object based off the data in a variable?

If it is , it would save a ton of coding.

public void CallMyFunction()
{
    try
    {
        var checkerList = conn.QueryAsync<FunctionList>("SELECT * FROM FunctionList",null );
        using (IEnumerator<FunctionList> enumer = checkerList.Result.GetEnumerator())
        {
            while (enumer.MoveNext())
            {
                FunctionList current = enumer.Current;
                string myFunctionToCall = current.FunctionName ;
                string myObjectToCall = current.FunctionObject;

                MethodInfo dynMethod = this.GetType().GetMethod(myFunctionToCall,
                BindingFlags.NonPublic | BindingFlags.Instance);
                dynMethod.Invoke(myObserverToCall, new object[] { myFunctionToCall });
            }
        }
    }
    catch
    {
    }
}
Migz
  • 163
  • 1
  • 13
  • Using `System.Reflection`, you can search for a method reference by name and invoke it with some arguments, yes. Look at (http://stackoverflow.com/a/135482/5296568, http://www.dotnetperls.com/getmethod). – Maximilian Gerhardt Jul 28 '16 at 11:48
  • Is the edit to the code on the correct path to what you ment ? – Migz Jul 28 '16 at 12:00
  • On the correct path, yes.Doesn't compile ofcourse because of the usage of non-defined variables `myObserverToCall`. You should read into Reflection if you want to do this stuff. Basically, it's a technique sothat you can dynamically search for methods in objects or classes (e.g. by name) and call them with some arguments. – Maximilian Gerhardt Jul 28 '16 at 12:02
  • Odd, it compiled fine, It accepted them as strings(the 2 lines up) .. ok will have a look, this will help so much when working – Migz Jul 28 '16 at 12:07
  • yeah I can't get this right .. – Migz Jul 28 '16 at 12:53

0 Answers0