Confused by Invoke
-
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
-
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
Neil Lamka wrote: mi.Invoke(null, args); You need to pass the object you going to invoke the method on. :)
-
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
Pass in the form object as the first parameter to invoke.