943,862 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 46298
  • C# RSS
Nov 23rd, 2007
0

Reflection Invoke Method w/ Params

Expand Post »
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.

C# Syntax (Toggle Plain Text)
  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:

C# Syntax (Toggle Plain Text)
  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:

C# Syntax (Toggle Plain Text)
  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:

C# Syntax (Toggle Plain Text)
  1. Object reference not set to instance of an object. It occurs on the line

And it occurs on the line:
C# Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 11
Solved Threads: 17
Junior Poster
mariocatch is offline Offline
103 posts
since Apr 2007
Mar 9th, 2008
0

Re: Reflection Invoke Method w/ Params

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bodikon is offline Offline
12 posts
since Jun 2006
Mar 9th, 2008
0

Re: Reflection Invoke Method w/ Params

Please, send me your project on RamyMahrous@gmail.com I'll debug it and tell you the problem en shaa ALLAH
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Jun 20th, 2008
0

Re: Reflection Invoke Method w/ Params

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.

C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
floydus is offline Offline
8 posts
since Dec 2006

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: Refer to an instance using a variable
Next Thread in C# Forum Timeline: How to Blink an image





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


Follow us on Twitter


© 2011 DaniWeb® LLC