Invoking a Method via Reflection Issue

J

Jeff

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(DataName.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
 
A

Arthur Dent

This should be possible using Reflection. I havent actually done this, but
the Reflection namespace is designed specifically for inspecting objects and
assemblies at runtime and accessing properties and methods.

This link looks like it gives a pretty good explanation:
http://www.csharphelp.com/archives/archive200.html. The CS file listed at
the bottom goes through step-by-step the code for his example. You should be
able to read the C# file pretty easily id think. Its basically just C++
syntax, and the higher-level concepts are the same pseudo-syntax as VB.NET.
If you need any help with any of it though, i could convert those parts.

Hope this helps. CheerZ!
 
J

Jeff Lieder

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/d...stemreflectionassemblyclassloadfiletopic2.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.
 

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