472,780 Members | 2,074 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 software developers and data experts.

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
2 7688
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Nelson P. Varghese | last post by:
Please have a look at the code given below. The class named 'Class1' has two methods with the same name but with different case. Now how can I invoke these methods from VB. using System; ...
1
by: Beenish Sahar Khan | last post by:
I'm using reflection to invoke some methods, now the general patteren of the function defination is private void FunctionName( argument1 , argument2) for some functions i don't need the...
0
by: Jeff | last post by:
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...
3
by: cartoper | last post by:
I am currently doing some R&D. The objective is to learn how to invoke methods via reflection using the InvokeMember method. The InvokeMember method throws an exception: Method...
10
by: schneider | last post by:
I'm looking for a way to programaticly call a method from a different object and associate the two objects at runtime. Example: Object A exist and is unknow, I want object B to be able to call a...
1
by: Suds | last post by:
Hi, I'm having an issue with invoking a Generic method that takes Generic Arguments. My method signature is public void GenericMethodWithGenericArguments<E, V>(List<EtheFirstList,...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
3
by: Rotsey | last post by:
Hi, I am getting a Exception has been thrown by the target of an invocation error when invoking a method Here is the crux of the code. I realise it could be a few things, any one give me...
2
by: =?Utf-8?B?SmltIE93ZW4=?= | last post by:
Hi John, Hopefully this post will find its way back to you - or perhaps be answered by someone else. As I mentioned in my last post on the earlier portion of this thread, changing the...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.