Re: Invoking a Method via Reflection Issue

Tech-Archive recommends: Speed Up your PC by fixing your registry

From: Arthur Dent (hitchhikersguideto-news_at_yahoo.com)
Date: 01/29/05


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
>
>



Relevant Pages

  • Re: Reflection - Need advice
    ... I used reflection recently when I had to send a text-based command to objects implementing ISignalTarget. ... As a consequence you are able to read those contents yourself, and also execute methods etc too. ... If you have assemblies in a "Plugins" folder of your app that you want loaded dynamically, maybe because other people are allowed to write their own. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: reposted: accessing private members through reflection in vstu
    ... "If the requested property is non-public and the caller does not have ReflectionPermission to reflect non-public objects outside the current assembly, this method returns a null reference." ... You don't actually control reflection permission as narrowly as that. ... You cannot grant specific permission for reflection on specific assemblies to specific assemblies -- you give the assembly permission to perform reflection based on grant sets. ...
    (microsoft.public.dotnet.framework)
  • Re: Using "custom attributes"
    ... do you know reflection well? ... and assemblies ... Attributes only make sense in conjunction with one or more features. ... 2)AspectDNG AOP Framework ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Using Reflection to Dynamically Determine Stored Procedure Name
    ... It is good to know that Reflection ... to know what stored procedures are being called by each aspx page. ... procs that are used by each of the three assemblies that are being ... then work my way down to the business/data layer assembly. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Number of classes in the .NET Framework
    ... // the reference, and when running in an aspx page, will require a fully ... The only problem I'm seeing with reflection ... >> assemblies, and used reflection to count up the classes and methods. ...
    (microsoft.public.dotnet.framework.aspnet)