Programaticly call a method from a different object?

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

    Programaticly call a method from a different object?

    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
    method on object A whenever needed.

    Like addhandler but I don't know what the method is and must discover it
    through reflection.

    Maybe something like this:

    Public Sub attach(ByVal valueA As Object)

    Dim meth() As MethodInfo = value.GetType.G etMethods

    Dim btnB As Button = New Button

    Dim mitem As MethodInfo = meth(0) 'find the method I want somehow

    AddHandler btnB.Click, mitem.[Selected_Method] '???

    End Sub

    Any ideas would be great.

    Thanks,

    Schneider




  • =?Utf-8?B?QmVu?=

    #2
    RE: Programaticly call a method from a different object?

    Dim obj As Object = Nothing
    Dim methods As MethodInfo() = obj.GetType().G etMethods()
    Dim method As MethodInfo = FindTheMethodYo uWant(methods)
    'store class-level reference to "method" and "obj"...
    mTheMethod = method
    mTheObject = object
    AddHandler btnB.Click, AddressOf DoTheMethod
    ....
    end sub

    private mTheMethod as Method
    private mTheObject as object

    sub DoTheMethod()
    mTheMethod.Invo ke(mTheObject, params) '(whatever the params are)
    end sub

    "schneider" wrote:
    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
    method on object A whenever needed.
    >
    Like addhandler but I don't know what the method is and must discover it
    through reflection.
    >
    Maybe something like this:
    >
    Public Sub attach(ByVal valueA As Object)
    >
    Dim meth() As MethodInfo = value.GetType.G etMethods
    >
    Dim btnB As Button = New Button
    >
    Dim mitem As MethodInfo = meth(0) 'find the method I want somehow
    >
    AddHandler btnB.Click, mitem.[Selected_Method] '???
    >
    End Sub
    >
    Any ideas would be great.
    >
    Thanks,
    >
    Schneider
    >
    >
    >
    >
    >

    Comment

    • schneider

      #3
      Re: Programaticly call a method from a different object?

      Thanks Ben,

      Was hoping for a cleaner approach, but that will work...

      Schneider

      "Ben" <Ben@discussion s.microsoft.com wrote in message
      news:0475B99A-4E9D-4DCC-A395-625EDCF803AC@mi crosoft.com...
      Dim obj As Object = Nothing
      Dim methods As MethodInfo() = obj.GetType().G etMethods()
      Dim method As MethodInfo = FindTheMethodYo uWant(methods)
      'store class-level reference to "method" and "obj"...
      mTheMethod = method
      mTheObject = object
      AddHandler btnB.Click, AddressOf DoTheMethod
      ...
      end sub
      >
      private mTheMethod as Method
      private mTheObject as object
      >
      sub DoTheMethod()
      mTheMethod.Invo ke(mTheObject, params) '(whatever the params are)
      end sub
      >
      "schneider" wrote:
      >
      >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
      >method on object A whenever needed.
      >>
      >Like addhandler but I don't know what the method is and must discover it
      >through reflection.
      >>
      >Maybe something like this:
      >>
      >Public Sub attach(ByVal valueA As Object)
      >>
      > Dim meth() As MethodInfo = value.GetType.G etMethods
      >>
      > Dim btnB As Button = New Button
      >>
      > Dim mitem As MethodInfo = meth(0) 'find the method I want somehow
      >>
      > AddHandler btnB.Click, mitem.[Selected_Method] '???
      >>
      >End Sub
      >>
      >Any ideas would be great.
      >>
      >Thanks,
      >>
      >Schneider
      >>
      >>
      >>
      >>
      >>

      Comment

      • Nick Malik [Microsoft]

        #4
        Re: Programaticly call a method from a different object?

        What is the objective? Do you want to be able to develop a plug-in for your
        code, after the system is released, and have your system recognize it?

        If so, that's covered loosly by traditional Design Patterns. I would
        recommend you investigate the Gang of Four Design Patterns, especially
        Strategy, Visitor, and Chain of Responsibility. (good reference on GoF
        patterns: http://home.earthlink.net/~huston2/dp/patterns.html )

        Once you understand the design patterns that lead to frameworks, go ahead
        and dive into a light framwork like Spring.Net. Here's an article on
        Dependency Injection in .Net:
        Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.



        --
        --- Nick Malik [Microsoft]
        MCSD, CFPS, Certified Scrummaster


        Disclaimer: Opinions expressed in this forum are my own, and not
        representative of my employer.
        I do not answer questions on behalf of my employer. I'm just a
        programmer helping programmers.
        --
        "schneider" <eschneider@sta rkinvestments.c emwrote in message
        news:%230j09yVQ HHA.488@TK2MSFT NGP06.phx.gbl.. .
        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 method on object A whenever needed.
        >
        Like addhandler but I don't know what the method is and must discover it
        through reflection.
        >
        Maybe something like this:
        >
        Public Sub attach(ByVal valueA As Object)
        >
        Dim meth() As MethodInfo = value.GetType.G etMethods
        >
        Dim btnB As Button = New Button
        >
        Dim mitem As MethodInfo = meth(0) 'find the method I want somehow
        >
        AddHandler btnB.Click, mitem.[Selected_Method] '???
        >
        End Sub
        >
        Any ideas would be great.
        >
        Thanks,
        >
        Schneider
        >
        >
        >
        >

        Comment

        • schneider

          #5
          Re: Programaticly call a method from a different object?

          No, I know how to do a plug-in design, and already using it.

          Trying to wire two methods together during runtime, similar to addhandler
          but method to be called is discovered by reflection.

          Currently using the Ben pattern, I just thought I might me missing cleaner
          language feature...

          Schneider


          "Nick Malik [Microsoft]" <nickmalik@hotm ail.nospam.comw rote in message
          news:0_2dnajF7c O_pyPYnZ2dnUVZ_ uKknZ2d@comcast .com...
          What is the objective? Do you want to be able to develop a plug-in for
          your code, after the system is released, and have your system recognize
          it?
          >
          If so, that's covered loosly by traditional Design Patterns. I would
          recommend you investigate the Gang of Four Design Patterns, especially
          Strategy, Visitor, and Chain of Responsibility. (good reference on GoF
          patterns: http://home.earthlink.net/~huston2/dp/patterns.html )
          >
          Once you understand the design patterns that lead to frameworks, go ahead
          and dive into a light framwork like Spring.Net. Here's an article on
          Dependency Injection in .Net:
          Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.

          >
          >
          --
          --- Nick Malik [Microsoft]
          MCSD, CFPS, Certified Scrummaster

          >
          Disclaimer: Opinions expressed in this forum are my own, and not
          representative of my employer.
          I do not answer questions on behalf of my employer. I'm just a
          programmer helping programmers.
          --
          "schneider" <eschneider@sta rkinvestments.c emwrote in message
          news:%230j09yVQ HHA.488@TK2MSFT NGP06.phx.gbl.. .
          >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 method on object A whenever needed.
          >>
          >Like addhandler but I don't know what the method is and must discover it
          >through reflection.
          >>
          >Maybe something like this:
          >>
          >Public Sub attach(ByVal valueA As Object)
          >>
          > Dim meth() As MethodInfo = value.GetType.G etMethods
          >>
          > Dim btnB As Button = New Button
          >>
          > Dim mitem As MethodInfo = meth(0) 'find the method I want somehow
          >>
          > AddHandler btnB.Click, mitem.[Selected_Method] '???
          >>
          >End Sub
          >>
          >Any ideas would be great.
          >>
          >Thanks,
          >>
          >Schneider
          >>
          >>
          >>
          >>
          >
          >

          Comment

          • Nick Malik [Microsoft]

            #6
            Re: Programaticly call a method from a different object?

            What's wrong with Visitor or Chain of Responsibility? You can set them up
            at runtime.

            --
            --- Nick Malik [Microsoft]
            MCSD, CFPS, Certified Scrummaster


            Disclaimer: Opinions expressed in this forum are my own, and not
            representative of my employer.
            I do not answer questions on behalf of my employer. I'm just a
            programmer helping programmers.
            --
            "schneider" <eschneider@sta rkinvestments.c emwrote in message
            news:eYUV0o$QHH A.4252@TK2MSFTN GP05.phx.gbl...
            No, I know how to do a plug-in design, and already using it.
            >
            Trying to wire two methods together during runtime, similar to addhandler
            but method to be called is discovered by reflection.
            >
            Currently using the Ben pattern, I just thought I might me missing cleaner
            language feature...
            >
            Schneider
            >
            >
            "Nick Malik [Microsoft]" <nickmalik@hotm ail.nospam.comw rote in message
            news:0_2dnajF7c O_pyPYnZ2dnUVZ_ uKknZ2d@comcast .com...
            >What is the objective? Do you want to be able to develop a plug-in for
            >your code, after the system is released, and have your system recognize
            >it?
            >>
            >If so, that's covered loosly by traditional Design Patterns. I would
            >recommend you investigate the Gang of Four Design Patterns, especially
            >Strategy, Visitor, and Chain of Responsibility. (good reference on GoF
            >patterns: http://home.earthlink.net/~huston2/dp/patterns.html )
            >>
            >Once you understand the design patterns that lead to frameworks, go ahead
            >and dive into a light framwork like Spring.Net. Here's an article on
            >Dependency Injection in .Net:
            >http://msdn.microsoft.com/msdnmag/is...s/default.aspx
            >>
            >>
            >--
            >--- Nick Malik [Microsoft]
            > MCSD, CFPS, Certified Scrummaster
            > http://blogs.msdn.com/nickmalik
            >>
            >Disclaimer: Opinions expressed in this forum are my own, and not
            >representati ve of my employer.
            > I do not answer questions on behalf of my employer. I'm just a
            >programmer helping programmers.
            >--
            >"schneider" <eschneider@sta rkinvestments.c emwrote in message
            >news:%230j09yV QHHA.488@TK2MSF TNGP06.phx.gbl. ..
            >>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 method on object A whenever needed.
            >>>
            >>Like addhandler but I don't know what the method is and must discover it
            >>through reflection.
            >>>
            >>Maybe something like this:
            >>>
            >>Public Sub attach(ByVal valueA As Object)
            >>>
            >> Dim meth() As MethodInfo = value.GetType.G etMethods
            >>>
            >> Dim btnB As Button = New Button
            >>>
            >> Dim mitem As MethodInfo = meth(0) 'find the method I want somehow
            >>>
            >> AddHandler btnB.Click, mitem.[Selected_Method] '???
            >>>
            >>End Sub
            >>>
            >>Any ideas would be great.
            >>>
            >>Thanks,
            >>>
            >>Schneider
            >>>
            >>>
            >>>
            >>>
            >>
            >>
            >
            >

            Comment

            • schneider

              #7
              Re: Programaticly call a method from a different object?

              Look Nick the pattern is not the issue, I know what I want to do, and Ben
              seems to understand, it's a language detail.

              Either read the prior messages or scram...



              "Nick Malik [Microsoft]" <nickmalik@hotm ail.nospam.comw rote in message
              news:aaidnUhMBZ OJ7iLYnZ2dnUVZ_ uKknZ2d@comcast .com...
              What's wrong with Visitor or Chain of Responsibility? You can set them up
              at runtime.
              >
              --
              --- Nick Malik [Microsoft]
              MCSD, CFPS, Certified Scrummaster

              >
              Disclaimer: Opinions expressed in this forum are my own, and not
              representative of my employer.
              I do not answer questions on behalf of my employer. I'm just a
              programmer helping programmers.
              --
              "schneider" <eschneider@sta rkinvestments.c emwrote in message
              news:eYUV0o$QHH A.4252@TK2MSFTN GP05.phx.gbl...
              >No, I know how to do a plug-in design, and already using it.
              >>
              >Trying to wire two methods together during runtime, similar to addhandler
              >but method to be called is discovered by reflection.
              >>
              >Currently using the Ben pattern, I just thought I might me missing
              >cleaner language feature...
              >>
              >Schneider
              >>
              >>
              >"Nick Malik [Microsoft]" <nickmalik@hotm ail.nospam.comw rote in message
              >news:0_2dnajF7 cO_pyPYnZ2dnUVZ _uKknZ2d@comcas t.com...
              >>What is the objective? Do you want to be able to develop a plug-in for
              >>your code, after the system is released, and have your system recognize
              >>it?
              >>>
              >>If so, that's covered loosly by traditional Design Patterns. I would
              >>recommend you investigate the Gang of Four Design Patterns, especially
              >>Strategy, Visitor, and Chain of Responsibility. (good reference on GoF
              >>patterns: http://home.earthlink.net/~huston2/dp/patterns.html )
              >>>
              >>Once you understand the design patterns that lead to frameworks, go
              >>ahead and dive into a light framwork like Spring.Net. Here's an article
              >>on Dependency Injection in .Net:
              >>http://msdn.microsoft.com/msdnmag/is...s/default.aspx
              >>>
              >>>
              >>--
              >>--- Nick Malik [Microsoft]
              >> MCSD, CFPS, Certified Scrummaster
              >> http://blogs.msdn.com/nickmalik
              >>>
              >>Disclaimer: Opinions expressed in this forum are my own, and not
              >>representativ e of my employer.
              >> I do not answer questions on behalf of my employer. I'm just a
              >>programmer helping programmers.
              >>--
              >>"schneider" <eschneider@sta rkinvestments.c emwrote in message
              >>news:%230j09y VQHHA.488@TK2MS FTNGP06.phx.gbl ...
              >>>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 method on object A whenever needed.
              >>>>
              >>>Like addhandler but I don't know what the method is and must discover
              >>>it through reflection.
              >>>>
              >>>Maybe something like this:
              >>>>
              >>>Public Sub attach(ByVal valueA As Object)
              >>>>
              >>> Dim meth() As MethodInfo = value.GetType.G etMethods
              >>>>
              >>> Dim btnB As Button = New Button
              >>>>
              >>> Dim mitem As MethodInfo = meth(0) 'find the method I want somehow
              >>>>
              >>> AddHandler btnB.Click, mitem.[Selected_Method] '???
              >>>>
              >>>End Sub
              >>>>
              >>>Any ideas would be great.
              >>>>
              >>>Thanks,
              >>>>
              >>>Schneider
              >>>>
              >>>>
              >>>>
              >>>>
              >>>
              >>>
              >>
              >>
              >
              >

              Comment

              • Keith Patrick

                #8
                Re: Programaticly call a method from a different object?

                Is the method set that B needs to call a known, finite set? If so, can you
                have A's type implement an interface? That's generally how a plug-in is
                implemented, but if you're talking about wiring up objects of any type based
                on configuration values (I have an app that does this because it has to wire
                up a remotable event-listening system but predates WS-*), then you've got to
                you reflection to get the MethodInfo and call Invoke. The call is
                A.GetType().Get Method("MethodN ame").Invoke(.. .)




                Comment

                • Nick Malik [Microsoft]

                  #9
                  Re: Programaticly call a method from a different object?

                  You completely missed the point, I'm afraid.

                  In most cases, reflection is not necessary. A better design removes the
                  need for the crutch. That's not to say that reflection is somehow bad, but
                  when used as an alternative to simple, easily described patterns that use
                  interfaces, declare intent, and are managed by the developer, and not 'self
                  modifying code', reflection can open the door to a wide array of design
                  issues that can make maintenance expensive.

                  I'm not criticizing the code. I'm expressing reservations about the need to
                  use it.

                  If a gentleman comes to my door with his arms full of packages, opening the
                  door is polite. If the packages are on fire, opening the door is quite
                  inconsiderate and potentially self-destructive. You have asked me to open
                  the door. I'm smelling smoke.

                  --
                  --- Nick Malik [Microsoft]
                  Enterprise Application Architect


                  Disclaimer: Opinions expressed in this forum are my own, and not
                  representative of my employer.
                  I do not answer questions on behalf of my employer. I'm just a
                  programmer helping programmers.
                  --
                  "schneider" <eschneider@sta rkinvestments.c emwrote in message
                  news:e43bjAJRHH A.2076@TK2MSFTN GP05.phx.gbl...
                  Look Nick the pattern is not the issue, I know what I want to do, and Ben
                  seems to understand, it's a language detail.
                  >
                  Either read the prior messages or scram...
                  >
                  >
                  >
                  "Nick Malik [Microsoft]" <nickmalik@hotm ail.nospam.comw rote in message
                  news:aaidnUhMBZ OJ7iLYnZ2dnUVZ_ uKknZ2d@comcast .com...
                  >What's wrong with Visitor or Chain of Responsibility? You can set them
                  >up at runtime.
                  >>
                  >--
                  >--- Nick Malik [Microsoft]
                  > MCSD, CFPS, Certified Scrummaster
                  > http://blogs.msdn.com/nickmalik
                  >>
                  >Disclaimer: Opinions expressed in this forum are my own, and not
                  >representati ve of my employer.
                  > I do not answer questions on behalf of my employer. I'm just a
                  >programmer helping programmers.
                  >--
                  >"schneider" <eschneider@sta rkinvestments.c emwrote in message
                  >news:eYUV0o$QH HA.4252@TK2MSFT NGP05.phx.gbl.. .
                  >>No, I know how to do a plug-in design, and already using it.
                  >>>
                  >>Trying to wire two methods together during runtime, similar to
                  >>addhandler but method to be called is discovered by reflection.
                  >>>
                  >>Currently using the Ben pattern, I just thought I might me missing
                  >>cleaner language feature...
                  >>>
                  >>Schneider
                  >>>
                  >>>
                  >>"Nick Malik [Microsoft]" <nickmalik@hotm ail.nospam.comw rote in message
                  >>news:0_2dnajF 7cO_pyPYnZ2dnUV Z_uKknZ2d@comca st.com...
                  >>>What is the objective? Do you want to be able to develop a plug-in for
                  >>>your code, after the system is released, and have your system recognize
                  >>>it?
                  >>>>
                  >>>If so, that's covered loosly by traditional Design Patterns. I would
                  >>>recommend you investigate the Gang of Four Design Patterns, especially
                  >>>Strategy, Visitor, and Chain of Responsibility. (good reference on GoF
                  >>>patterns: http://home.earthlink.net/~huston2/dp/patterns.html )
                  >>>>
                  >>>Once you understand the design patterns that lead to frameworks, go
                  >>>ahead and dive into a light framwork like Spring.Net. Here's an
                  >>>article on Dependency Injection in .Net:
                  >>>http://msdn.microsoft.com/msdnmag/is...s/default.aspx
                  >>>>
                  >>>>
                  >>>--
                  >>>--- Nick Malik [Microsoft]
                  >>> MCSD, CFPS, Certified Scrummaster
                  >>> http://blogs.msdn.com/nickmalik
                  >>>>
                  >>>Disclaimer : Opinions expressed in this forum are my own, and not
                  >>>representati ve of my employer.
                  >>> I do not answer questions on behalf of my employer. I'm just a
                  >>>programmer helping programmers.
                  >>>--
                  >>>"schneider " <eschneider@sta rkinvestments.c emwrote in message
                  >>>news:%230j09 yVQHHA.488@TK2M SFTNGP06.phx.gb l...
                  >>>>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 method on object A whenever needed.
                  >>>>>
                  >>>>Like addhandler but I don't know what the method is and must discover
                  >>>>it through reflection.
                  >>>>>
                  >>>>Maybe something like this:
                  >>>>>
                  >>>>Public Sub attach(ByVal valueA As Object)
                  >>>>>
                  >>>> Dim meth() As MethodInfo = value.GetType.G etMethods
                  >>>>>
                  >>>> Dim btnB As Button = New Button
                  >>>>>
                  >>>> Dim mitem As MethodInfo = meth(0) 'find the method I want somehow
                  >>>>>
                  >>>> AddHandler btnB.Click, mitem.[Selected_Method] '???
                  >>>>>
                  >>>>End Sub
                  >>>>>
                  >>>>Any ideas would be great.
                  >>>>>
                  >>>>Thanks,
                  >>>>>
                  >>>>Schneider
                  >>>>>
                  >>>>>
                  >>>>>
                  >>>>>
                  >>>>
                  >>>>
                  >>>
                  >>>
                  >>
                  >>
                  >
                  >

                  Comment

                  • schneider

                    #10
                    Re: Programaticly call a method from a different object?

                    No the method is not know, in fact it's unknown user/developer code/library,
                    and the reflection that I'm using are things used by the framework for
                    similar things. As I explained earlier, a plug-in pattern does not work for
                    me in this case, but I use it for other things. This needs to be loosely
                    bound to be of any value to a developer.

                    Schneider


                    "Keith Patrick" <richard_keith_ patrick@nospam. hotmail.comwrot e in message
                    news:eFAbHCNRHH A.4088@TK2MSFTN GP06.phx.gbl...
                    Is the method set that B needs to call a known, finite set? If so, can you
                    have A's type implement an interface? That's generally how a plug-in is
                    implemented, but if you're talking about wiring up objects of any type
                    based on configuration values (I have an app that does this because it has
                    to wire up a remotable event-listening system but predates WS-*), then
                    you've got to you reflection to get the MethodInfo and call Invoke. The
                    call is A.GetType().Get Method("MethodN ame").Invoke(.. .)
                    >
                    >
                    >
                    >

                    Comment

                    • Keith Patrick

                      #11
                      Re: Programaticly call a method from a different object?

                      In that case, I think GetType().GetMe thod().Invoke() would be your best
                      route. As Nick mentioned in another part of the thread, though, you're
                      putting a lot of trust into what you're calling, basically becoming an
                      operating system/runtime in itself, so I'd recommend restricting the
                      permissions that are allowed in that execution call (run it in a .Net
                      security-based sandbox within your app)




                      Comment

                      Working...