Found my answer after a bit of searching. Guess you can't use the
"loadfile" method if you are wanting to execute methods. Changed the calls
to "LoadFrom" and all is well. Microsoft description below.
http://msdn.microsoft.com/library/de...filetopic2.asp
Use the LoadFile method to load and examine assemblies that have the same
identity, but are located in different paths. Do not use LoadFile to load
assemblies that you want to execute. LoadFile does not load files into the
LoadFrom context, and does not resolve dependencies using the load path, as
the LoadFrom method does. LoadFile is useful in this limited scenario
because LoadFrom cannot be used to load assemblies that have the same
identities but different paths; it will load only the first such assembly.
"Jeff" <no****@hotmail.com> wrote in message
news:uc**************@tk2msftngp13.phx.gbl...
I am trying to dynamically load an assembly via reflection and then invoke
a method of that assembly that will populate a custom type collection passed
into the method byref. I am able to dynamically load both the DALC
component (for the method call) and the Entity component (for the custom
type collection to pass in), but I keep getting an error ( Message "Object
type cannot be converted to target type.") when invoking the method. If I
add a reference to my project and create the collection type like, "Dim
myColl as New Entity.StatesCollection" as opposed to doing it with "Dim
mycoll As Object = collAssembly.CreateInstance("Entity." & DataName.Trim &
"Collection)" all works well and my byref parameter is populated. I
assume this has to do with creating my byref parameter dynamically via
reflection, but can someone explain to me why this is happening?
Thanks,
Jeff
Private Function GetComboData(ByVal DataName As String) As Object
Try
Dim dalcAssembly As System.Reflection.Assembly =
System.Reflection.Assembly.LoadFile("C:\Dal\Xml\" & DataName.Trim &
"\Dal.Xml." & DataName.Trim & ".dll")
Dim collAssembly As System.Reflection.Assembly =
System.Reflection.Assembly.LoadFile("C:\Entity\" & DataName.Trim &
"\Entity." & DataName.Trim & ".dll")
Dim mycoll As Object = collAssembly.CreateInstance("Entity." &
DataName.Trim & "Collection)
Dim dalc As Object = dalcAssembly.CreateInstance("Dal.Xml." &
DataName.Trim & "_XML_DALC")
Dim myMethods() As MethodInfo = dalc.GetType.GetMethods()
Dim myMethod As MethodInfo
For Each myMethod In myMethods
If myMethod.Name = "Load" Then
Dim myParms() As ParameterInfo = myMethod.GetParameters
Dim myParm As ParameterInfo
For Each myParm In myParms
If
myParm.ParameterType.ToString.ToUpper.IndexOf(Data Name.ToUpper.Trim &
"COLLECTION") > -1 Then
myMethod.Invoke(dalc, New Object()
{CType("C:\Script\states.xml", String), mycoll, CType(False, Boolean)})
Exit For
End If
Next
End If
Next
Catch ex As Exception
Stop
End Try
End Function