-2

I'm retrieving a list of methods from a class

            foreach (MethodInfo item in typeof(RptInfo).GetMethods())
        {
            if (item.ReturnType.Equals(typeof(DataTable)))
            {
                lstMethods.Items.Add((item.Name));
            }
        }

Clearly adding the method names to a list control.

I'm having problem, I guess more correctly, not understanding, the proper use of 'Action or 'Func' in order to call the selected method name and getting it's return value into a dataview control.

I did reference: How to call a Method from String using c#?

but still not certain as to a correct implementation method. Still new to implementing these concepts

qball96
  • 1
  • 2
  • why would you want it to do with reflection? – Roelant M Jul 11 '17 at 20:43
  • Your code will give you all methods, this will include those with parameters, how would you expect to call those? – DavidG Jul 11 '17 at 20:50
  • I've limited the methods returned by type In this case. Also, none of the Methods take parameters. Although figuring that out would be a nice to know – qball96 Jul 12 '17 at 00:53

1 Answers1

0

First, note that the typeof(RptInfo).GetMethods() will give you both instance and static methods, with any number of parameters. To call any of these methods, you'll need to supply the instance (unless the method is static), and values for the parameters. I suggest you look into overloads of the GetMethods method if you want to get methods only with specific signatures.

Let's say you have an instance of the RptInfo on which you want to run your methods, and you have enough values to provide for their parameters. Then, when you select your item from the list, you should call something like

DataTable result = typeof(RptInfo)
     .GetMethod(lstMethods.SelectedItem.ToString())
     .Invoke(instance, new object[]{value1,value2}) 
     as DataTable

Again, look into overloads of the GetMethod method in case you might have methods with the same name but different signatures. In the Invoke call, you'll use null instead of the instance if your method is static, and you will have to provide the proper number of properly typed values to satisfy the method's signature.

LiborV
  • 164
  • 4