Generic method parser / invoker

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Generic method parser / invoker

 
0
  #1
May 27th, 2005
I'm writing a utility program. In fact, the program will be a Console application that will contain many static methods, each method being a specific utility.

The application will take two command line arguments: filename, and utility name. Of course I want to run the method specified on the file specified.

I want to discuss approaches to this. Each method may have a unique signature and return value/type.

Thinking through this, I need a way to

1) check to see if the method on the commandline exists in the program. I could create an array of all method names, and check against the array. Simple enough, but is there a more elegant way? Does a class contain knowledge of its own members?

2) invoke any particular method, given a string. That string would be the method name.

How would I know what parameters I need to pass to the method? They wouldn't come from the commandline. I need some way of knowing, gathering, and passing in all required parameters/objects.

What I'm thinking is that the Main() method will declare a FileStream, plus any other objects that may be needed by any of the utility methods. I need some way to "map" these objects to the parameters needed by the method I want to call.

Again, I could create some data structure that said "if arg[1] equals this string, then call this method with these parameters". The problem is that becomes more to maintain. When a new utility is added to the program, I'll have to write the utility method, PLUS define any new objects the utility may need in Main(), PLUS update the structure that maps arg[1] to the new method. I'm looking for clever ways to avoid that final step.

I'm looking for other ideas, pointers to articles, general discussion, anything that would help me to think through this project.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: Generic method parser / invoker

 
0
  #2
May 27th, 2005
It looks like the System.Reflection class has everything I might need. As this develops, I'll post code. I'd still like any input anyone has to offer.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: Generic method parser / invoker

 
0
  #3
May 27th, 2005
Why does it always seem that when I post C# threads, I'm only talking to myself? In any case, I've hit my first snag. I've reached the point where I want to invoke a public, static method that's defined in the current class. All the System.Reflection examples I've found are for invoking methods from a different class, or an assembly, or on instance objects. How do you invoke a STATIC (ie, "class method") method of the CURRENT class?

Code:

  1. using System;
  2. using System.IO;
  3. using System.Collections;
  4. using System.Reflection;
  5.  
  6. namespace pcl_util
  7. {
  8. /// <summary>
  9. /// Summary description for Class1.
  10. /// </summary>
  11. class Class1
  12. {
  13. static string filename;
  14. static string function;
  15. static FileStream baseFS;
  16.  
  17.  
  18. [STAThread]
  19. static void Main(string[] args)
  20. {
  21. // get filename to process, if found, declare a FileStream
  22. filename = args[0].ToString();
  23. if (File.Exists(filename))
  24. {
  25. baseFS = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read, 8192);
  26. }
  27. else
  28. {
  29. Console.WriteLine("FILE NOT FOUND: " + filename);
  30. }
  31.  
  32. // does function exist in this program? If so, get its parameters and run it
  33. function = args[1].ToString();
  34. if (checkFunction(function))
  35. {
  36. Type myType = typeof(Class1);
  37. MethodInfo myMethodInfo = myType.GetMethod(function);
  38. ParameterInfo[] myParameters = myMethodInfo.GetParameters();
  39.  
  40. // collect parameters, call method.
  41. object[] methodParams = new Object [myParameters.Length];
  42. int mpIndex = 0;
  43. foreach( ParameterInfo paramInfo in myParameters)
  44. {
  45. if (paramInfo.ParameterType == Type.GetType("System.IO.FileStream"))
  46. {
  47. methodParams[mpIndex] = baseFS;
  48. }
  49.  
  50. mpIndex++;
  51. }
  52.  
  53. myType.InvokeMember(function,
  54. BindingFlags.DeclaredOnly |
  55. BindingFlags.Public |
  56. BindingFlags.InvokeMethod, null, null, methodParams);
  57.  
  58. }
  59. else
  60. {
  61. Console.WriteLine("FUNCTION NOT FOUND: " + function);
  62. }
  63. }
  64.  
  65. public static bool checkFunction(string f)
  66. {
  67. Type myType = typeof(Class1);
  68. MethodInfo myMethodInfo = myType.GetMethod(f);
  69.  
  70. if (myMethodInfo == null)
  71. {
  72. return false;
  73. }
  74. else
  75. {
  76. return true;
  77. }
  78. }
  79.  
  80. public static System.Int32 getFF(FileStream fs)
  81. {
  82. // get the byte position of all FF in file
  83. Console.WriteLine("getFF ran.");
  84.  
  85. return 0;
  86. }
  87. }
  88. }

The problem is in the InvokeMethod() method. The second "null" in the parameter list is supposed to be an object. What object? The only object is the currently running instance of the current class. I'm confused.

For the command line arguments, I'm passing in a valid filename, and the string "getFF". The above code is supposed to invoke the getFF() method.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: Generic method parser / invoker

 
0
  #4
May 27th, 2005
On the other hand, this code works fine:

  1. Type myType = typeof(Class1);
  2. MethodInfo myMethodInfo = myType.GetMethod(function);
  3. ParameterInfo[] myParameters = myMethodInfo.GetParameters();
  4.  
  5. // collect parameters, call method.
  6. object[] methodParams = new Object [myParameters.Length];
  7. int mpIndex = 0;
  8. foreach( ParameterInfo paramInfo in myParameters)
  9. {
  10. if (paramInfo.ParameterType == Type.GetType("System.IO.FileStream"))
  11. {
  12. methodParams[mpIndex] = baseFS;
  13. }
  14.  
  15. mpIndex++;
  16. }
  17.  
  18. object retval = myType.GetMethod(function).Invoke(null, methodParams);

The question is, "why?". The invoke method calls for two parameters, object, and the method parameters, as an object array.

I've passed in "null" as the object, because there is NOT an instance object. All methods are static. The only instance is the currently running program. Is "null" correct, then, or is there a proper way to refer to the currently running program?
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: Generic method parser / invoker

 
0
  #5
May 27th, 2005
Another little snag: how does one dynamically declare an object to be of a certain type?

In other words, MethodInfo.ReturnType tells you the defined return type of the method. So how do you declare a return object/variable of that type?
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 3
Reputation: abhisek.verma is an unknown quantity at this point 
Solved Threads: 0
abhisek.verma abhisek.verma is offline Offline
Newbie Poster

Re: Generic method parser / invoker

 
0
  #6
Mar 8th, 2007
how does one dynamically declare an object to be of a certain type?
Abhishek Verma
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Generic method parser / invoker

 
0
  #7
Mar 10th, 2007
Originally Posted by abhisek.verma View Post
how does one dynamically declare an object to be of a certain type?
Do you mean generics? I.e say you wanted to create a stack but you didn't know if it was a going to be a stack of integer/strings? I guess you would use generics?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC