Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Confused by Invoke

Confused by Invoke

Scheduled Pinned Locked Moved C#
comxmlhelp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Neil Lamka
    wrote on last edited by
    #1

    I am trying to play around with automating menu creation through external XML files. I've used several of the examples found in Code Project and I'm trying to bind the menu items to the MagicLibrary menu classes. I'm getting close...but I'm stumped by trying to invoke a method in the calling application from my menu helper class. What I have is the following The menu helper class is created by the main application and it reads the xml file and creates all the MagicLibrary MenuCommand items..that all seems to work fine. Within my menu helper class I have hooked into the menu click event and have the following method. The method retrieves a previously found and saved MethodInfo item that matches the actual method I want to call in the main application private void menu_Click(object sender, System.EventArgs e) { object[] args = {sender, (object)e}; MethodInfo mi = (MethodInfo)onclickList[ (string)mc.Tag+".Click" ]; if (null != mi) mi.Invoke(null, args); } The method I'm trying to invoke in the main application looks like the following public void menuNew_Click(object sender, System.EventArgs e) { //* Code to do stuff in my application based on the menu click //* event } What I get is an unhandled exception An unhandled exception of type 'System.Reflection.TargetException' occurred in mscorlib.dll Additional information: Non-static method requires a target. Which has me stumped. I'm obviously not doing something right but I don't know what I'm missing. Any help would be appreciated. Thanks Neil Lamka neil@meetingworks.com

    L W 2 Replies Last reply
    0
    • N Neil Lamka

      I am trying to play around with automating menu creation through external XML files. I've used several of the examples found in Code Project and I'm trying to bind the menu items to the MagicLibrary menu classes. I'm getting close...but I'm stumped by trying to invoke a method in the calling application from my menu helper class. What I have is the following The menu helper class is created by the main application and it reads the xml file and creates all the MagicLibrary MenuCommand items..that all seems to work fine. Within my menu helper class I have hooked into the menu click event and have the following method. The method retrieves a previously found and saved MethodInfo item that matches the actual method I want to call in the main application private void menu_Click(object sender, System.EventArgs e) { object[] args = {sender, (object)e}; MethodInfo mi = (MethodInfo)onclickList[ (string)mc.Tag+".Click" ]; if (null != mi) mi.Invoke(null, args); } The method I'm trying to invoke in the main application looks like the following public void menuNew_Click(object sender, System.EventArgs e) { //* Code to do stuff in my application based on the menu click //* event } What I get is an unhandled exception An unhandled exception of type 'System.Reflection.TargetException' occurred in mscorlib.dll Additional information: Non-static method requires a target. Which has me stumped. I'm obviously not doing something right but I don't know what I'm missing. Any help would be appreciated. Thanks Neil Lamka neil@meetingworks.com

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      Neil Lamka wrote: mi.Invoke(null, args); You need to pass the object you going to invoke the method on. :)

      leppie::AllocCPArticle(Generic DFA State Machine for .NET);

      1 Reply Last reply
      0
      • N Neil Lamka

        I am trying to play around with automating menu creation through external XML files. I've used several of the examples found in Code Project and I'm trying to bind the menu items to the MagicLibrary menu classes. I'm getting close...but I'm stumped by trying to invoke a method in the calling application from my menu helper class. What I have is the following The menu helper class is created by the main application and it reads the xml file and creates all the MagicLibrary MenuCommand items..that all seems to work fine. Within my menu helper class I have hooked into the menu click event and have the following method. The method retrieves a previously found and saved MethodInfo item that matches the actual method I want to call in the main application private void menu_Click(object sender, System.EventArgs e) { object[] args = {sender, (object)e}; MethodInfo mi = (MethodInfo)onclickList[ (string)mc.Tag+".Click" ]; if (null != mi) mi.Invoke(null, args); } The method I'm trying to invoke in the main application looks like the following public void menuNew_Click(object sender, System.EventArgs e) { //* Code to do stuff in my application based on the menu click //* event } What I get is an unhandled exception An unhandled exception of type 'System.Reflection.TargetException' occurred in mscorlib.dll Additional information: Non-static method requires a target. Which has me stumped. I'm obviously not doing something right but I don't know what I'm missing. Any help would be appreciated. Thanks Neil Lamka neil@meetingworks.com

        W Offline
        W Offline
        Wesner Moise
        wrote on last edited by
        #3

        Pass in the form object as the first parameter to invoke.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups