Problems loading assembly using System.Reflection.assembly

  • Thread starter TheVillageCodingIdiot
  • Start date
T

TheVillageCodingIdiot

I have 2 dll's that exist in different folders and a WinForm that
loads CLassB. Im loading the assemblys at runtime using
System.Reflection. ClassA is going to be used for multable apps so I
would like to keep it in a central location and just have the various
apps reference it. ClassB inherits ClassA. When i created ClassB
project, I added the reference to ClassA and set "Copy Local" = false.
Now when I load Class B with the below code I get a error "Unable to
load one or more of the requested types". But if i copy ClassA dll
into the same folder as ClassB it works fine. How do I get the ClassB
dll to see ClassA dll without it being in the same folder?

here is the code for using for reflection
Private Function GetBillingClass(ByVal classPath As String, ByVal
className As String) As Object
Dim returnObject As Object
Dim asm As System.Reflection.Assembly
Dim I() As Type
Dim billingReport As String = ""

Try
If Not System.IO.File.Exists(classPath) Then
MessageBox.Show("Reporting class does not exist",
"ReturnBillingClass")
Return Nothing
Exit Function
End If

asm = System.Reflection.Assembly.LoadFrom(classPath)
I = asm.GetTypes()

For j As Integer = 0 To I.GetUpperBound(0)
'Console.WriteLine(I(j).FullName)
If I(j).Name = className Then
billingReport = I(j).FullName
End If
Next

If billingReport <> "" Then
Dim hasStart As Boolean = False
Dim hasSetSQL As Boolean = False

Dim billMeth As System.Reflection.MethodInfo()
billMeth = asm.GetType(billingReport).GetMethods

For J As Integer = 0 To billMeth.GetUpperBound(0)
'Console.WriteLine(billMeth(J).Name)
If billMeth(J).Name.ToLower = "start" Then
hasStart = True
If billMeth(J).Name.ToLower = "setsqlconn" Then
hasSetSQL = True
Next

If hasStart And hasSetSQL Then
returnObject = asm.CreateInstance(billingReport)
Else
MessageBox.Show("The required methods are not in
this class. Please verify the appropriate methods are available", _
"ReturnBillingClass", MessageBoxButtons.OK,
MessageBoxIcon.Error)

returnObject = Nothing
End If
Else
MessageBox.Show("Unable to find requested billing
report class", "ReturnBillingClass", MessageBoxButtons.OK,
MessageBoxIcon.Error)
returnObject = Nothing
End If

Catch ex As ExeptionClass
MessageBox.Show(ex.Message, "ReturnBillingClass",
MessageBoxButtons.OK, MessageBoxIcon.Error)
returnObject = Nothing
End Try

Return returnObject

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top