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