-3
for (int tsid = 1; tsid < controller.getRowCount(currentTest); tsid++)
{
    // values from xls
    keyword = controller.getCellData(currentTest, "Keyword", tsid);
    //object=controller.getCellData(currentTest, "Object", tsid);
    currentTSID = controller.getCellData(currentTest, "TSID", tsid);
    stepDescription = controller.getCellData(currentTest, "Description", tsid);

    Console.WriteLine("Keyword is:" + keyword);

    try
    {
        // --this is equivalent java code 
        //MethodInfo method= Keywords.class.getMethod(keyword);

        MethodInfo method= method.GetMethodBody(keyword);
        String result = (String)method.Invoke(method);

        if(!result.StartsWith("Fail")) {
            ReportUtil.addKeyword(stepDescription, keyword, result,null);
        }
    }
    catch (...) { ... }
}

Here from excel sheet we are reading the Keyword and we need to call that specific method using Reflection:

MethodInfo method= method.GetMethodBody(keyword);
String result = (String)method.Invoke(method);

But these two lines of code are throwing me some syntax error. I have used using System.Reflection; at the top of the file, but the error persists.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
Nilanjan Saha
  • 135
  • 2
  • 11
  • `MethodInfo method = method.GetMethodBody(keyword)` can't work because you're using `method` before it's initialized. What are you trying to do at that line? – CodeCaster Jan 04 '14 at 19:57
  • -1: "some syntax error" is not good explanation of the error. Side note: if you select error message in Visual Studio and click "F1" you'll get much more detailed explanation of the error from MSDN. If you still don't understand the error - post exact error message (along with error code like `CS0123` or link to corresponding MSDN article). – Alexei Levenkov Jan 04 '14 at 20:59

3 Answers3

1

Don't pass the MethodInfo Object method to the invoke call but instead the object on which you want to call the method. I can't see the object you probably could do this on.

Furthermore Invoke has two parameters (see MSDN). So the syntax error is probably that you forgot to pass the parameters.

As far as I understand your code you have an Excel sheet holding some method names which you want to call dynamically. Right? But you can't just get a .NET Object from a Excel cell.

If you need an object to call the method on, you'll need to create one and establish the correct state to call it. So you could probably add some more data to your excel sheet and use it to set up the object.

thuri
  • 302
  • 1
  • 2
  • 10
1

In C# you don't use Type.class, instead you use typeof(Type).

You can use this in combination with GetMethod(string methodName) to get a specific MethodInfo, which you can then Invoke(object instance, object[] parameters). For static classes object instance should be null.

For example:

 typeof(Console).GetMethod("ReadLine").Invoke(null, new object[] { });
Aidiakapi
  • 6,034
  • 4
  • 33
  • 62
0

May be for a Future reader, can use something like this..

keyWordHolder program = new keyWordHolder();
MethodInfo[] methods = typeof(keyWordHolder).GetMethods();
foreach (MethodInfo meth in methods)
 {
   if (meth.Name == keywords)
      {
          meth.Invoke(program, null); 
       }   
deW1
  • 5,562
  • 10
  • 38
  • 54
Aniish
  • 1
  • 1