reflection question

  • Thread starter Thread starter Bob
  • Start date Start date
B

Bob

I need to be able to remotely load all referenced assemblies of an
executable. The example code below works fine when all referenced assemblies
are either 1. copied locally or 2. registered in the GAC. But if you have
enough DLL's in a hierarchy, copying all references locally cause them to
trip over each other which is a major pain in the ass. And I don't want to
have to register everything in the GAC. Could someone help me get this
loading routine to work for non-GAC assemblies that aren't local? All I need
is to figure out which path the top-level assembly is first looking for each
referenced assembly.

Thanks very much to anyone who can help,
Bob

-------------------------------

Imports System.Reflection

Module Main

Public Sub main()
Dim path As String = 'file path goes here
Dim file As String = 'exe name goes here
LoadAssembliesFromFile(path, file)
End Sub

Public Sub LoadAssembliesFromFile( _
ByVal OutputPath As String, ByVal OutputFileName As String)
Try
Dim asm As [Assembly] = _
System.Reflection.Assembly.LoadFrom(OutputPath & OutputFileName)
For Each asmname As AssemblyName In _
asm.GetReferencedAssemblies
Try
If Not IsFrameworkAssembly(asmname.FullName) Then
asm.Load(asmname)
MsgBox("assembly " & asmname.FullName _
& " successfully loaded.")
End If
Catch ex As System.IO.FileNotFoundException
MsgBox(ex.ToString)
End Try
Next
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

Public Function IsFrameworkAssembly( _
ByVal AsmFullName As String) As Boolean
Select Case GetAssemblyPublicKeyToken(AsmFullName)
Case "b77a5c561934e089", "b03f5f7f11d50a3a"
Return True
End Select
End Function

Public Function GetAssemblyPublicKeyToken( _
ByVal AsmFullName As String) As String
Dim pkt As String = "PublicKeyToken="
Return AsmFullName.Substring(AsmFullName.IndexOf(pkt) + pkt.Length)
End Function

End Module
 
OK Never mind. I forgot, you have to copy non-GAC assmblies locally or it
just won't run.

I settled on pooling all the DLL output directories into one relative path -
"..\OUTPUT\", and that seems to do the trick as long as I don't set "copy
local" to False in any of the projects. I get the added benefit of not
having to browse around to different areas to add references to new
projects, and also no longer have DLL's duplicated everywhere.

Bob
 
Back
Top