Re: Invoking a Method via Reflection Issue
From: Arthur Dent (hitchhikersguideto-news_at_yahoo.com)
Date: 01/29/05
- Next message: J. Freeman: "VB.net Please help"
- Previous message: Arthur Dent: "Re: MDI Tab fprms"
- In reply to: Jeff: "Invoking a Method via Reflection Issue"
- Next in thread: Jeff Lieder: "Re: Invoking a Method via Reflection Issue"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 28 Jan 2005 19:38:09 -0500
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" <nospam@hotmail.com> wrote in message
news:ucdsWnLBFHA.1084@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(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
>
>
- Next message: J. Freeman: "VB.net Please help"
- Previous message: Arthur Dent: "Re: MDI Tab fprms"
- In reply to: Jeff: "Invoking a Method via Reflection Issue"
- Next in thread: Jeff Lieder: "Re: Invoking a Method via Reflection Issue"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|