Reflection Invoke Method w/ Params

Please support our C# advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
Reply

Join Date: Apr 2007
Posts: 103
Reputation: mariocatch is an unknown quantity at this point 
Solved Threads: 17
mariocatch mariocatch is offline Offline
Junior Poster

Reflection Invoke Method w/ Params

 
0
  #1
Nov 23rd, 2007
Hi All,

A long time ago I researched Reflection and created a solution demoing my learnt capabilities from it.

In my example I did not pass any arguments into the Invocation of the method call using reflection.

I went to tackle this and I seem to be hitting a few dead ends.

Here is the method I call from my Main method in another class.

  1. public void RunClass(string dllName, string className, string methodName, params Object[] parameters)
  2. {
  3. // Create the assemblies from our current DLL.
  4. Assembly _Assemblies = Assembly.LoadFrom(dllName);
  5.  
  6. // Get the type that we want from the assemblies.
  7. // IE: This would be the fully qualified class name (including namespace)
  8. // Example: "Reflectionism.Examples.Example1" or "Reflectionism.Examples.Example2"
  9. Type _Type = null;
  10. try
  11. {
  12. _Type = _Assemblies.GetType(className);
  13. }
  14. catch (Exception ex)
  15. {
  16. Console.WriteLine("\n\nError - couldn't obtain classrd from " + className);
  17. Console.WriteLine("EXCEPTION OUTPUT\n" + ex.Message + "\n" + ex.InnerException);
  18. return;
  19. }
  20.  
  21. // Get the desired method we want from the target type.
  22. MethodInfo _MethodInfo = null;
  23. try
  24. {
  25. _MethodInfo = _Type.GetMethod(methodName, new Type[] { typeof(Object[]) });
  26. }
  27. catch (Exception ex)
  28. {
  29. Console.WriteLine("\n\nError - couldn't obtain method " + methodName + " from " + className);
  30. Console.WriteLine("EXCEPTION OUTPUT\n" + ex.Message + "\n" + ex.InnerException);
  31. return;
  32. }
  33.  
  34. // The first parameter to pass into the Invoke Method coming up.
  35. Object _InvokeParam1 = Activator.CreateInstance(_Type);
  36. //Object[] _Params = new Object[] { parameters };
  37.  
  38. // This calls the target method ("DisplayMyself").
  39. // NOTE: I'm not passing any arguments down to the method being invoked.
  40. // Therefore, I'm passing null as my argument, otherwise Invoke takes an
  41. // array of Objects.
  42. _MethodInfo.Invoke(_InvokeParam1, new Object[] { _parameters});
  43. }

I call this RunClass method from my Main method like this:

  1. //_UserInputParam is a Console.ReadLine() object (In my testing I am passing '5' to this call).
  2. _Main._ReflectMain.RunClass(_UserInputDLL, _UserInputClass, _UserInputMethod, Int32.Parse(_UserInputParam));

The class that is being called by the invocation of the reflection method call is set up like this:

  1. public void DisplayMyself(params Object[] parameters)
  2. {
  3. if (parameters.Length <= 0)
  4. {
  5. Console.WriteLine("Hello! I am " + this.ToString());
  6. return;
  7. }
  8. Console.WriteLine("Parameter Found in: " + this.ToString() + "__ Param was: ");
  9. for (int i = 0; i < parameters.Length; i++)
  10. {
  11. Console.WriteLine(parameters[i]);
  12. }
  13. }

The error I'm receiving is:

  1. Object reference not set to instance of an object. It occurs on the line

And it occurs on the line:
  1. _MethodInfo.Invoke(_InvokeParam1, new Object[] { parameters });

Even though when debugging parameters, it has the object '5' within it (or whatever the user inputs into the console.readline (and outputted through int32.parse() call).


Any help/insight would be appreciated.

Thanks,
-Catch
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 12
Reputation: bodikon is an unknown quantity at this point 
Solved Threads: 0
bodikon bodikon is offline Offline
Newbie Poster

Re: Reflection Invoke Method w/ Params

 
0
  #2
Mar 9th, 2008
Hey man, I am having the same issue, if youknow of a solution let me know, otherwise i'll keep searching and let you know if I do
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Reflection Invoke Method w/ Params

 
0
  #3
Mar 9th, 2008
Please, send me your project on RamyMahrous@gmail.com I'll debug it and tell you the problem en shaa ALLAH
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 8
Reputation: floydus is an unknown quantity at this point 
Solved Threads: 0
floydus floydus is offline Offline
Newbie Poster

Re: Reflection Invoke Method w/ Params

 
0
  #4
Jun 20th, 2008
I've been working with c# since only a year, but I figured this for a c# windows service I've made that calls a vb6 class object with no less then 52 arguments....

hope it will help someone.

  1. using System;
  2. using System.Reflection;
  3.  
  4. private Type ProgType = Type.GetTypeFromProgID("myvb6dll.dvrObj"); // Assign Type to the Class Type
  5. object o = Activator.CreateInstance(ProgType);
  6. object returnValue;
  7.  
  8. // Create object that will holds arguments to pass to InvokeMember
  9. // See VB6 Newobject project for list of arguments
  10. object[] arguments = new object[52];
  11. arguments[0] = objectClass;
  12. .......
  13. .......
  14. arguments[51] = objectVerifyThis;
  15.  
  16. returnValue = ProgType.InvokeMember("makeCall", BindingFlags.InvokeMethod, null, o, arguments);
  17. resFonction = returnValue.ToString();

Let me know if it's clear enough...
Thanks.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC