VB .Net - Reflection: Reflected Method from a loaded Assembly executes before calling method. Why?

Posted by pu.griffin on Stack Overflow See other posts from Stack Overflow or by pu.griffin
Published on 2010-05-17T20:55:08Z Indexed on 2010/05/17 21:40 UTC
Read the original article Hit count: 342

Filed under:
|

When I am loading an Assembly dynamically, then calling a method from it, I appear to be getting the method from Assembly executing before the code in the method that is calling it. It does not appear to be executing in a Serial manner as I would expect. Can anyone shine some light on why this might be happening. Below is some code to illustrate what I am seeing, the code from the some.dll assembly calls a method named PerformLookup. For testing I put a similar MessageBox type output with "PerformLookup Time: " as the text. What I end up seeing is:

First: "PerformLookup Time: 40:842"

Second: "initIndex Time: 45:873"

Imports System
Imports System.Data
Imports System.IO
Imports Microsoft.VisualBasic.Strings
Imports System.Reflection

Public Class Class1
    Public Function initIndex(indexTable as System.Collections.Hashtable) As System.Data.DataSet
        Dim writeCode As String
        MessageBox.Show("initIndex Time: " & Date.Now.Second.ToString() & ":" & Date.Now.Millisecond.ToString())
        System.Threading.Thread.Sleep(5000)
        writeCode = RefreshList()
    End Function

    Public Function RefreshList() As String   
        Dim asm As System.Reflection.Assembly
        Dim t As Type()
        Dim ty As Type
        Dim m As MethodInfo()
        Dim mm As MethodInfo
        Dim retString as String
        retString = ""

        Try
            asm = System.Reflection.Assembly.LoadFrom("C:\Program Files\some.dll")
            t = asm.GetTypes()
            ty = asm.GetType(t(28).FullName) 'known class location
            m = ty.GetMethods()
            mm = ty.GetMethod("PerformLookup")
            Dim o as Object
            o = Activator.CreateInstance(ty)
            Dim oo as Object()   
            retString = mm.Invoke(o,Nothing).ToString()
        Catch Ex As Exception       
        End Try

        return retString
    End Function
End Class

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about reflection