943,984 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 11068
  • C# RSS
May 27th, 2005
0

Generic method parser / invoker

Expand Post »
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.
Similar Threads
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
May 27th, 2005
0

Re: Generic method parser / invoker

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.
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
May 27th, 2005
0

Re: Generic method parser / invoker

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:

C# Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
May 27th, 2005
0

Re: Generic method parser / invoker

On the other hand, this code works fine:

C# Syntax (Toggle Plain Text)
  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?
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
May 27th, 2005
0

Re: Generic method parser / invoker

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?
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
Mar 8th, 2007
0

Re: Generic method parser / invoker

how does one dynamically declare an object to be of a certain type?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
abhisek.verma is offline Offline
3 posts
since Feb 2007
Mar 10th, 2007
0

Re: Generic method parser / invoker

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?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Math.round?
Next Thread in C# Forum Timeline: Internet Application





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC