| | |
Reflection Invoke Method w/ Params
Please support our C# advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
![]() |
•
•
Join Date: Apr 2007
Posts: 103
Reputation:
Solved Threads: 17
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.
I call this RunClass method from my Main method like this:
The class that is being called by the invocation of the reflection method call is set up like this:
The error I'm receiving is:
And it occurs on the line:
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
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)
public void RunClass(string dllName, string className, string methodName, params Object[] parameters) { // Create the assemblies from our current DLL. Assembly _Assemblies = Assembly.LoadFrom(dllName); // Get the type that we want from the assemblies. // IE: This would be the fully qualified class name (including namespace) // Example: "Reflectionism.Examples.Example1" or "Reflectionism.Examples.Example2" Type _Type = null; try { _Type = _Assemblies.GetType(className); } catch (Exception ex) { Console.WriteLine("\n\nError - couldn't obtain classrd from " + className); Console.WriteLine("EXCEPTION OUTPUT\n" + ex.Message + "\n" + ex.InnerException); return; } // Get the desired method we want from the target type. MethodInfo _MethodInfo = null; try { _MethodInfo = _Type.GetMethod(methodName, new Type[] { typeof(Object[]) }); } catch (Exception ex) { Console.WriteLine("\n\nError - couldn't obtain method " + methodName + " from " + className); Console.WriteLine("EXCEPTION OUTPUT\n" + ex.Message + "\n" + ex.InnerException); return; } // The first parameter to pass into the Invoke Method coming up. Object _InvokeParam1 = Activator.CreateInstance(_Type); //Object[] _Params = new Object[] { parameters }; // This calls the target method ("DisplayMyself"). // NOTE: I'm not passing any arguments down to the method being invoked. // Therefore, I'm passing null as my argument, otherwise Invoke takes an // array of Objects. _MethodInfo.Invoke(_InvokeParam1, new Object[] { _parameters}); }
I call this RunClass method from my Main method like this:
C# Syntax (Toggle Plain Text)
//_UserInputParam is a Console.ReadLine() object (In my testing I am passing '5' to this call). _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)
public void DisplayMyself(params Object[] parameters) { if (parameters.Length <= 0) { Console.WriteLine("Hello! I am " + this.ToString()); return; } Console.WriteLine("Parameter Found in: " + this.ToString() + "__ Param was: "); for (int i = 0; i < parameters.Length; i++) { Console.WriteLine(parameters[i]); } }
The error I'm receiving is:
C# Syntax (Toggle Plain Text)
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)
_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
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
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
•
•
Join Date: Dec 2006
Posts: 8
Reputation:
Solved Threads: 0
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.
Let me know if it's clear enough...
Thanks.
hope it will help someone.
C# Syntax (Toggle Plain Text)
using System; using System.Reflection; private Type ProgType = Type.GetTypeFromProgID("myvb6dll.dvrObj"); // Assign Type to the Class Type object o = Activator.CreateInstance(ProgType); object returnValue; // Create object that will holds arguments to pass to InvokeMember // See VB6 Newobject project for list of arguments object[] arguments = new object[52]; arguments[0] = objectClass; ....... ....... arguments[51] = objectVerifyThis; returnValue = ProgType.InvokeMember("makeCall", BindingFlags.InvokeMethod, null, o, arguments); resFonction = returnValue.ToString();
Let me know if it's clear enough...
Thanks.
![]() |
Similar Threads
- Threading, Delegates and the Invoke method. (VB.NET)
- Retreiving/Allocating a new type during runtime with Reflection (C#)
- Generic method parser / invoker (C#)
- Invoking (Java)
- I need help..urgently (VB.NET)
- buzz game (Java)
Other Threads in the C# Forum
- Previous Thread: Refer to an instance using a variable
- Next Thread: How to Blink an image
| Thread Tools | Search this Thread |
.net access algorithm array bitmap box buttons c# check checkbox client clock combobox connection control conversion csharp database databaseconnection datagrid datagridview datagridviewcheckbox dataset datetime degrees draganddrop drawing dynamiccreation encryption enum equation excel file form format formatting forms function gdi+ hospitalmanagementinformationsystem hospitalmanagementsystems httpwebrequest image input install java label list math microsystems mouseclick mysql namevaluepairs operator path photoshop picturebox pixelinversion post powerpacks programming property radians regex remoting richtextbox running... serialization server sleep soap socket sql sqlserver stack statistics string table teamsuite temperature text textbox thread time timer update uploadatextfile usercontrol validation visualstudio wait webbrowser whileloop windows windowsformsapplication winforms working wpf write xml






