Passing Values Through Reflection

Nate Bross

Contributor
Joined
Apr 6, 2005
Messages
601
Location
Chicago, IL
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.

Visual Basic:
'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
 
You need to tell the methodInfo the types of the input parameters.

Try this:
Visual Basic:
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]
 
Last edited:
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
Visual Basic:
Dim ob As Object() = Nothing
 
ob(0) = DirectCast(DateTime.Now().ToString(), Object
with
Visual Basic:
'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
Visual Basic:
'Easier syntax: Dim Variable As Type() = {item, item, item...}
Dim ob As Object() = {DateTime.Now().ToString()}
 
Back
Top