Reflection question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JMZ

    #1

    Reflection question

    I want to look up method names in a database which will be present in the
    executing object and use the names to find the method address to pass to the
    addressof operator at runtime.

    Is that possible? If so, what reflection classes enable me to do that?

    Thanks.
  • Mattias Sjögren

    #2
    Re: Reflection question

    [color=blue]
    >I want to look up method names in a database which will be present in the
    >executing object and use the names to find the method address to pass to the
    >addressof operator at runtime.[/color]

    So in other words you want to create a delegate dynamically.

    [color=blue]
    >Is that possible? If so, what reflection classes enable me to do that?[/color]

    System.Delegate .CreateDelegate ()



    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.

    Comment

    • JMZ

      #3
      Re: Reflection question

      Thanks, but that did not work. CreateDelegate throws an exception when I try
      to get the type of my custom type, using Type.GetType.

      Here is what I want to do:

      Private OneStep As StepSub
      Delegate Sub StepSub()
      Private Steps(ALL_STEPS ) As StepSub

      Eventually, I will use the Combine method on Steps:

      OneStep = OneStep.Combine (Steps)

      And then call all the subs contained in the original Steps array:

      OneStep.Dynamic Invoke(Nothing)

      Before I do that however, I want to build the original super list (Steps)
      based on a database table which contains the method names and their
      corresponding sequence (index into the Steps array). At runtime, I will have
      a structure to tell me which of the steps to leave in the Steps array and
      which ones to remove (by setting the corresponding Steps entry to Nothing).

      Everything works just fine except that I want to be able to build the Steps
      array at runtime with no prior knowledge of the methods. Which means I want
      to use a string representation of the method name and turn it into a method
      address compatible with the delegate type of my methods to pass to the
      AddressOf operator (or in some way resolve to a compatible value).

      So instead of:

      Steps(0) = New StepSub(Address Of FirstMethod)
      Steps(1) = New StepSub(Address Of SecondMethod)
      Steps(2) = New StepSub(Address Of ThirdMethod)
      ....
      Steps(n)= New StepSub(Address Of LasMethod)

      I want to write:
      Dim ds as DataSet = GetMyListOfMeth odsAndSequences
      Dim dr as DataRow

      For Each dr in ds.Tables(0).Ro ws

      Steps(dr.Item(" MethodStepIndex ")) = new StepSub(Address Of
      SomeWayToGetThe AddressOfTheMet hodWhoseNameIsI nThisDataRow)

      Next

      Hopefully, this mess makes sense to you.

      Thanks again


      "Mattias Sjögren" wrote:
      [color=blue]
      >[color=green]
      > >I want to look up method names in a database which will be present in the
      > >executing object and use the names to find the method address to pass to the
      > >addressof operator at runtime.[/color]
      >
      > So in other words you want to create a delegate dynamically.
      >
      >[color=green]
      > >Is that possible? If so, what reflection classes enable me to do that?[/color]
      >
      > System.Delegate .CreateDelegate ()
      >
      >
      >
      > Mattias
      >
      > --
      > Mattias Sjögren [MVP] mattias @ mvps.org
      > http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
      > Please reply only to the newsgroup.
      >[/color]

      Comment

      • Mattias Sjögren

        #4
        Re: Reflection question

        >Thanks, but that did not work. CreateDelegate throws an exception when I try[color=blue]
        >to get the type of my custom type, using Type.GetType.[/color]

        Can you show us that part of your code, and tell us what kind of
        exception you get?

        Since you have compile-time knowledge about what type of delegate to
        create, you can use VB's GetType operator instead of the Type.GetType
        method.



        Mattias

        --
        Mattias Sjögren [MVP] mattias @ mvps.org
        http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
        Please reply only to the newsgroup.

        Comment

        • JMZ

          #5
          Re: Reflection question

          Mattias,

          Thanks very much! The VB GetType works like a champ and the code runs
          exactly as I intended.

          Many thanks!

          "Mattias Sjögren" wrote:
          [color=blue][color=green]
          > >Thanks, but that did not work. CreateDelegate throws an exception when I try
          > >to get the type of my custom type, using Type.GetType.[/color]
          >
          > Can you show us that part of your code, and tell us what kind of
          > exception you get?
          >
          > Since you have compile-time knowledge about what type of delegate to
          > create, you can use VB's GetType operator instead of the Type.GetType
          > method.
          >
          >
          >
          > Mattias
          >
          > --
          > Mattias Sjögren [MVP] mattias @ mvps.org
          > http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
          > Please reply only to the newsgroup.
          >[/color]

          Comment

          Working...