VB.Net - Reflection: Reflected Method from loaded assembly is executed before method call. What for?
When I load an assembly dynamically and then call a method from within it, I seem to be getting a method from the assembly, executed before the code in the method that calls it. It doesn't seem to execute in a sequential order as I expected. Can anyone please explain why this might be happening. Below is some code to illustrate what I see, the code from the some.dll assembly calls a method named PerformLookup. For testing purposes, I posted a similar output of the MessageBox type with the text "PerformLookup Time:". Eventually I see:
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
I added a flush statement to the end of my write method, even though I had a StreamReader.Close () call. Same results. Here are some results from the debug statements I put in to try and diagnose this further. In initIndex I have
write(timestamp)
save(file)
write(save success)
write(saved value)
write(timestamp)
write(file create / file modified times)
sleep(5 seconds)
invoke(assembly method)
write(timestamp)
And in the build method I have:
write(timestamp)
sleep(5 seconds) //yes, two 5 second sleeps between write and read
read(file)
write(file value)
write(timestamp)
write(file create / file write times)
Here is my output from the initIndex log file:
17:732
True
A/P
17:732
5/17/2010 11:59:30 AM / 5/18/2010 7:49:17 AM
22:748
And here is the output from the Assembly log file:
12:670
CASH
17:685
5/17/2010 11:59:30 AM / 5/18/2010 7:41:20 AM
a source to share