Use the extension method to Clone the menu.

Source: Internet
Author: User

 

I used C #2.0 all the time. This time I used C #4. 0 to develop MongoDB tools, because I used VB, which is also the debut of C #4.

I have always known the extension method of C #. I used it for the first time today ......

The tool we are working on is a traditional desktop tool. during the production process, we found that the context menu item and the main menu item ToolStripMenuItem do not have the Clone function. In this case, context menu items and main menu items cannot be shared.

After reading some examples on the internet, I just created a new class that inherits ToolStripMenuItem and then copied the attributes. My method is to expand the method, and the idea is similar:

1 using System;

2 using System. Collections. Generic;

3 using System. Linq;

4 using System. Text;

5 using System. Windows. Forms;

6 using System. Reflection;

7 using System. ComponentModel;

8 namespace MagicMongoDBTool. Module

9 {

10 static class CloneMeunToolItem

11 {

12 /// <summary>

13 // copy the menu item

14 /// </summary>

15 /// <param name = "OrgMenuItem"> </param>

16 /// <returns> </returns>

17 public static ToolStripMenuItem Clone (this ToolStripMenuItem OrgMenuItem)

18 {

19 ToolStripMenuItem CloneMenuItem = new ToolStripMenuItem ();

20 //!!! The typeof parameter must be the basic class of ToolStripMenuItem !!! If you use Control, you cannot get the value !!!

21 // thanks to beargo, a csdn user, for posting [How to obtain the method name customized for the event ?] In the prompt, the online example does not illustrate this problem.

22 /// ah ........

23 Delegate [] _ List = GetObjectEventList (OrgMenuItem, "EventClick", typeof (ToolStripItem ));

24 CloneMenuItem. Click + = new EventHandler (

25 (x, y) =>{_ List [0]. DynamicInvoke (x, y );}

26 );

27 CloneMenuItem. Text = OrgMenuItem. Text;

28 return CloneMenuItem;

29}

30

31 /// <summary>

32 // get control event zgke@sina.com qq: 116149

33 /// </summary>

34 // <param name = "p_Control"> Object </param>

35 // <param name = "p_EventName"> for event name EventClick EventDoubleClick, you need to check the name of the control. event class. </param>

36 // <param name = "p_EventType"> If the WINFROM Control uses typeof (Control) </param>

37 // <returns> delegate column </returns>

38 public static Delegate [] GetObjectEventList (object p_Object, string p_EventName, Type p_EventType)

39 {

40 PropertyInfo _ PropertyInfo = p_Object.GetType (). GetProperty ("Events", BindingFlags. Instance | BindingFlags. NonPublic );

41 if (_ PropertyInfo! = Null)

42 {

43 object _ EventList = _ PropertyInfo. GetValue (p_Object, null );

44 if (_ EventList! = Null & _ EventList is EventHandlerList)

45 {

46 EventHandlerList _ List = (EventHandlerList) _ EventList;

47 FieldInfo _ FieldInfo = p_EventType.GetField (p_EventName, BindingFlags. Static | BindingFlags. NonPublic | BindingFlags. IgnoreCase );

48 if (_ FieldInfo = null) return null;

49 Delegate _ ObjectDelegate = _ List [_ FieldInfo. GetValue (p_Object)];

50 if (_ ObjectDelegate = null) return null;

51 return _ ObjectDelegate. GetInvocationList ();

52}

53}

54 return null;

55}

56}

57}

I just want to talk about a lot of code on the network, including many answers on CSDN. Basically, the same example is used.

However, there is no explanation for this example. In particular, if the WINFROM Control is used, typeof (Control) is used ). If you follow this, the menu item cannot be used.

The correct statement is to use the base class of the control.

 

The following describes the progress of the MongoDB tool:

Today, we have completed the data display function in three ways.

 


Index management

 

 

Website address http://www.magicdict.com/interested in writing to me root # magicdict.com [convert # to @] or add MSN mynightelfplayer@hotmail.com

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.