Nate Bross Posted April 29, 2006 Posted April 29, 2006 I know there's gotta be a simple answer to this, but it's evading me. I would like to pass a value to a method that I am invoking through System.Reflection. I have a windows forms application that calls an external dll and invokes it's TestIt method. I can call the method without any values; but I would like to pass values to it. Here's what I have that isn't working. if MakeString doesn't take any values it works as expected. 'This is in the Windows Forms App. Public Function MakeString(ByVal sdate As Object) As String Return "The Time is: " & sdate.ToString() End Function 'This is the dll Public Class Class1 Public Sub TestIt() System.Console.WriteLine("Class code Worked!") Dim x As New IO.StreamWriter("c:\testdll.txt") x.WriteLine("Class Code Worked") Dim Assm As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly Dim Obj As Object = Assm.CreateInstance("CustomBinary.Form1", True) Dim Meth As Reflection.MethodInfo = Obj.GetType.GetMethod("MakeString", True) Dim ob As Object() = Nothing ob(0) = DirectCast(DateTime.Now().ToString(), Object) x.WriteLine(Meth.Invoke(Obj, ob)) x.Flush() x.Close() End Sub End Class Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
Nate Bross Posted April 29, 2006 Author Posted April 29, 2006 Oh yeah, I'm using Visual Stuido 2005 (Framework 2.0). Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
Mike_R Posted April 30, 2006 Posted April 30, 2006 (edited) You need to tell the methodInfo the types of the input parameters. Try this:Dim Assm As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly Dim obj As Form = CType(Assm.CreateInstance("CustomBinary.Form1", True), Form) Dim paramsType As Type() = New Type(0) {} paramsType(0) = Type.GetType("System.String") Dim Meth As Reflection.MethodInfo = obj.GetType.GetMethod("MakeString", paramsType) Dim args As Object() = New Object(0) {} ' <-- Corrected as per Marble Eater, below. args(0) = DateTime.Now().ToString() X.WriteLine (Meth.Invoke(obj, args)) You should also think about using 'Option Strict On', it will really help you out... Hope this helps! Mike [Edit: I corrected one of the lines as per Marble Eater's suggestion's below. -- MR] Edited April 30, 2006 by Mike_R Quote Posting Guidelines Avatar by Lebb
Leaders snarfblam Posted April 30, 2006 Leaders Posted April 30, 2006 Mike's suggestion is probably the best if you know which overload you want ahead of time. Also, in both code listings you are creating a null reference to an object array and then assign values to its elements. I don't know if that was the actual code you had or if you retyped it and made a mistake, but replace Dim ob As Object() = Nothing ob(0) = DirectCast(DateTime.Now().ToString(), Object with 'Use the (New Type(ubound) {}) syntax to create an array. 'Make sure you have the right number of elements, in this case 'one for one parameter Dim ob As Object() = New Object(0) {} 'This is a "narrowing" conversion. You don't need to explicitly convert anything 'to an object because everything already is an object. ob(0) = DateTime.Now().ToString() or 'Easier syntax: Dim Variable As Type() = {item, item, item...} Dim ob As Object() = {DateTime.Now().ToString()} Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.