Reflection and Dynamic Methods

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

    Reflection and Dynamic Methods

    I have a situation where I have an object name as a string, and a method as
    a string. I need to construct a click handler with those two bits of
    information. This is what I came up with so far... and the error message I
    am receiving...

    Assembly assem = Assembly.GetExe cutingAssembly( );
    string currentNamespac e =
    Assembly.GetExe cutingAssembly( ).GetTypes()[0].Namespace;
    Type type = assem.GetType(c urrentNamespace + "." + m_FormName, true, true);
    MethodInfo methinf = type.GetMethod( MethodCall);
    m_ButtonControl .Click += new CommandEventHan dler(methinf);

    Error 1 'methinf' is a 'variable' but is used like a 'method'

    Thanks,
    Ron

  • Paul E Collins

    #2
    Re: Reflection and Dynamic Methods

    "Ron" <rs_herhuth@yah oo.comwrote:
    m_ButtonControl .Click += new CommandEventHan dler(methinf);
    Error 1 'methinf' is a 'variable' but is used like a 'method'
    I'm not sure what CommandEventHan dler is, but this way works for me in
    Windows Forms:

    MethodInfo methinf = type.GetMethod( "NameOfMethod") ;
    theButton.Click += new EventHandler(Me thodCall);

    Eq.


    Comment

    • Paul E Collins

      #3
      Re: Reflection and Dynamic Methods

      Oh, ignore the previous post. I made a mistake.

      Eq.


      Comment

      • Ignacio Machin ( .NET/ C# MVP )

        #4
        Re: Reflection and Dynamic Methods

        On Apr 16, 1:35 pm, "Paul E Collins" <find_my_real_a ddr...@CL4.org>
        wrote:
        "Ron" <rs_herh...@yah oo.comwrote:
        m_ButtonControl .Click += new CommandEventHan dler(methinf);
        Error 1 'methinf' is a 'variable' but is used like a 'method'
        >
        I'm not sure what CommandEventHan dler is, but this way works for me in
        Windows Forms:
        It's a delegate, to be used in the onCommand event in web controls

        Comment

        • Ignacio Machin ( .NET/ C# MVP )

          #5
          Re: Reflection and Dynamic Methods

          On Apr 16, 12:58 pm, "Ron" <rs_herh...@yah oo.comwrote:
          I have a situation where I have an object name as a string, and a method as
          a string.  I need to construct a click handler with those two bits of
          information.  This is what I came up with so far... and the error message I
          am receiving...
          >
          Assembly assem = Assembly.GetExe cutingAssembly( );
          string currentNamespac e =
          Assembly.GetExe cutingAssembly( ).GetTypes()[0].Namespace;
          Type type = assem.GetType(c urrentNamespace + "." +  m_FormName, true, true);
          MethodInfo methinf = type.GetMethod( MethodCall);
          m_ButtonControl .Click += new CommandEventHan dler(methinf);
          >
          Error 1 'methinf' is a 'variable' but is used like a 'method'
          >
          Thanks,
          Ron
          Hi,

          Take a look at this code:
          string targetMethodNam e = "yourmethod ";
          string eventName = "eventName" ;

          //Hook the event.
          System.Reflecti on.EventInfo evClick =
          c.GetType().Get Event(eventName );
          Type tDelegate = evClick.EventHa ndlerType;
          Delegate d = Delegate.Create Delegate(tDeleg ate,
          Field.ContentPr ovider, targetMethodNam e);
          System.Reflecti on.MethodInfo addHandler =
          evClick.GetAddM ethod();
          Object[] addHandlerArgs = { d };
          addHandler.Invo ke(c, addHandlerArgs) ;

          Comment

          • Ron

            #6
            Re: Reflection and Dynamic Methods

            Ignacio,

            Good Stuff!

            One question: What is Field.ContentPr ovider?

            I'm getting an ArgumentExcepti on : Error Binding to target method on this
            line:
            Delegate d = Delegate.Create Delegate(tDeleg ate, methinf);

            Thanks!
            Ron

            "Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin @gmail.comwrote in
            message
            news:f7a55ec9-77af-4b61-8c01-b1e86dfa5fce@e6 7g2000hsa.googl egroups.com...
            On Apr 16, 12:58 pm, "Ron" <rs_herh...@yah oo.comwrote:
            I have a situation where I have an object name as a string, and a method
            as
            a string. I need to construct a click handler with those two bits of
            information. This is what I came up with so far... and the error message I
            am receiving...
            >
            Assembly assem = Assembly.GetExe cutingAssembly( );
            string currentNamespac e =
            Assembly.GetExe cutingAssembly( ).GetTypes()[0].Namespace;
            Type type = assem.GetType(c urrentNamespace + "." + m_FormName, true,
            true);
            MethodInfo methinf = type.GetMethod( MethodCall);
            m_ButtonControl .Click += new CommandEventHan dler(methinf);
            >
            Error 1 'methinf' is a 'variable' but is used like a 'method'
            >
            Thanks,
            Ron
            Hi,

            Take a look at this code:
            string targetMethodNam e = "yourmethod ";
            string eventName = "eventName" ;

            //Hook the event.
            System.Reflecti on.EventInfo evClick =
            c.GetType().Get Event(eventName );
            Type tDelegate = evClick.EventHa ndlerType;
            Delegate d = Delegate.Create Delegate(tDeleg ate,
            Field.ContentPr ovider, targetMethodNam e);
            System.Reflecti on.MethodInfo addHandler =
            evClick.GetAddM ethod();
            Object[] addHandlerArgs = { d };
            addHandler.Invo ke(c, addHandlerArgs) ;

            Comment

            • Ignacio Machin ( .NET/ C# MVP )

              #7
              Re: Reflection and Dynamic Methods

              On Apr 16, 2:14 pm, "Ron" <rs_herh...@yah oo.comwrote:
              Ignacio,
              >
              Good Stuff!
              >
              One question: What is Field.ContentPr ovider?
              >

              Sorry, that is part of my code :) , take a look at
              Delegate.Create Delegate method to know the parameter expected there,
              and replace it with your value, IIRC it's the instance object that
              will be hooked to tha event

              Comment

              • Ron

                #8
                Re: Reflection and Dynamic Methods


                I checked out the documentation and its even less clear :-)

                The Form that the method is in is called frmTest.

                I tried Delegate d = Delegate.Create Delegate(tDeleg ate,frmTest,met hinf);

                and I get an Error binding to target method on that line.

                Ron


                "Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin @gmail.comwrote in
                message
                news:2ef741ee-0b56-49dd-97ee-4a1fb7d77c01@26 g2000hsk.google groups.com...
                On Apr 16, 2:14 pm, "Ron" <rs_herh...@yah oo.comwrote:
                Ignacio,
                >
                Good Stuff!
                >
                One question: What is Field.ContentPr ovider?
                >

                Sorry, that is part of my code :) , take a look at
                Delegate.Create Delegate method to know the parameter expected there,
                and replace it with your value, IIRC it's the instance object that
                will be hooked to tha event

                Comment

                • Ignacio Machin ( .NET/ C# MVP )

                  #9
                  Re: Reflection and Dynamic Methods

                  On Apr 16, 3:39 pm, "Ron" <rs_herh...@yah oo.comwrote:
                  I checked out the documentation and its even less clear :-)
                  >
                  The Form that the method is in is called frmTest.
                  >
                  I tried  Delegate d = Delegate.Create Delegate(tDeleg ate,frmTest,met hinf);
                  It's easy , my code use this overload:
                  Delegate.Create Delegate (Type, Object, String)

                  As you see, the first parameter is a Type instance, that I created in
                  the line above
                  Type tDelegate = evClick.EventHa ndlerType;

                  The second parameter is the instance of the class that will handle the
                  event ( in your case would be an isntance of the frmTest)
                  and finally the last parameter is the name of the method (its a member
                  of frmTest)

                  Comment

                  • Ron

                    #10
                    Re: Reflection and Dynamic Methods

                    Ignacio,

                    Im Still getting the problem... here is my code:

                    public Janus.Windows.R ibbon.ButtonCom mand CreateButton()
                    {
                    m_ButtonControl = new Janus.Windows.R ibbon.ButtonCom mand();
                    m_ButtonControl .Text = m_ButtonText;
                    if (m_ImageName.Tr im().Length 0)
                    {
                    m_ButtonControl .LargeImage =
                    (Image)rm.GetOb ject(m_ImageNam e);
                    }
                    Assembly assem = Assembly.GetExe cutingAssembly( );
                    string currentNamespac e =
                    Assembly.GetExe cutingAssembly( ).GetTypes()[0].Namespace;
                    Type type = assem.GetType(c urrentNamespace + "." + m_FormName,
                    true, true);


                    MethodInfo methinf = type.GetMethod( MethodCall);

                    System.Reflecti on.EventInfo evClick =
                    m_ButtonControl .GetType().GetE vent("Click");
                    Type tDelegate = evClick.EventHa ndlerType;
                    Delegate d = Delegate.Create Delegate(tDeleg ate, type,
                    MethodCall);
                    System.Reflecti on.MethodInfo addHandler =
                    evClick.GetAddM ethod();
                    Object[] addHandlerArgs = { d };
                    //addHandler.Invo ke(c, addHandlerArgs) ;

                    return m_ButtonControl ;
                    }


                    "Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin @gmail.comwrote in
                    message
                    news:a0495ded-6466-4142-9f02-aba2fe9a9aa1@l2 8g2000prd.googl egroups.com...
                    On Apr 16, 3:39 pm, "Ron" <rs_herh...@yah oo.comwrote:
                    I checked out the documentation and its even less clear :-)
                    >
                    The Form that the method is in is called frmTest.
                    >
                    I tried Delegate d = Delegate.Create Delegate(tDeleg ate,frmTest,met hinf);
                    It's easy , my code use this overload:
                    Delegate.Create Delegate (Type, Object, String)

                    As you see, the first parameter is a Type instance, that I created in
                    the line above
                    Type tDelegate = evClick.EventHa ndlerType;

                    The second parameter is the instance of the class that will handle the
                    event ( in your case would be an isntance of the frmTest)
                    and finally the last parameter is the name of the method (its a member
                    of frmTest)

                    Comment

                    • Ignacio Machin ( .NET/ C# MVP )

                      #11
                      Re: Reflection and Dynamic Methods

                      On Apr 16, 4:47 pm, "Ron" <rs_herh...@yah oo.comwrote:
                      Ignacio,
                      >
                      Im Still getting the problem... here is my code:
                      What error are you getting now?
                      It should be different than the first ime

                      Comment

                      Working...