Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Posted (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 by Mike_R

Posting Guidelines

 

Avatar by Lebb

  • Leaders
Posted

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()}

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...