467,915 Members | 1,173 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,915 developers. It's quick & easy.

Invoking a Method via Reflection Issue

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
Nov 21 '05 #1
  • viewed: 7452
Share:
2 Replies
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!
"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

Nov 21 '05 #2
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

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Nelson P. Varghese | last post: by
1 post views Thread by Beenish Sahar Khan | last post: by
3 posts views Thread by cartoper | last post: by
3 posts views Thread by Rotsey | last post: by
2 posts views Thread by =?Utf-8?B?SmltIE93ZW4=?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.