title: VB .Net - Reflection: Reflected Method from a loaded Assembly executes before calling method. Why?
body: <p>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:</p>
  
  <p><code>First: "PerformLookup Time: 40:842"</code></p>
  
  <p><code>Second: "initIndex Time: 45:873"</code></p>
  
  <pre><code>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: " &amp; Date.Now.Second.ToString() &amp; ":" &amp; 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
  </code></pre>
  
  <p>I added a flush statement to the end of my write method even though I had a StreamReader.Close() call.  Same results.  Here is some output from debug statements I put in to try and diagnose this further.  In initIndex I have  </p>
  
  <pre><code>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)  
  </code></pre>
  
  <p>And in the Assembly method I have:</p>
  
  <pre><code>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)  
  </code></pre>
  
  <p>Here is my output from initIndex logfile:</p>
  
  <pre><code>17:732  
  True  
  A/P  
  17:732  
  5/17/2010 11:59:30 AM / 5/18/2010 7:49:17 AM  
  22:748  
  </code></pre>
  
  <p>And here is the output from the Assembly class logfile:</p>
  
  <pre><code>12:670  
  CASH  
  17:685  
  5/17/2010 11:59:30 AM / 5/18/2010 7:41:20 AM  
  </code></pre>