.NET 4.x Как оно работает (контекстное меню)? - C#
Формулировка задачи:
Всем, добрый день, программисты. Очень рад общению с Вами
Я новичок в Вашем деле... и хочу стать таким настоящим программистом как Вы
мне не понятен как работает нижеприведенный код работы ContextDataRow (контекстное меню для таблиц)
особо не ясно такие строки:
Вы извините что так много... но я надеюсь на Вашу помощь
using System; using System.Data; using System.Windows.Forms; using System.Data.SqlClient; using System.Reflection; public class ContextDataRow : DataRow { public ContextDataRow(DataRowBuilder builder) : base(builder) { } public void PopupMenu(System.Windows.Forms.Control parent, int x, int y) { // Использовать рефлексию, чтобы получить список команд // для всплывакхцего меню MemberInfo[] members = this.GetType().FindMembers (MemberTypes.Method, BindingFlags.Public | BindingFlags.Instance, new System.Reflection.MemberFilter(Filter), null); if (members.Length > 0) { // Создать контекстное меню ContextMenu menu = new ContextMenu(); // Теперь пройти по членам и сгенерировать всплывающее меню. // Обратите внимание на приведение к MethodInfo в цикле foreach foreach (MethodInfo meth in members) { // Получить заголовок операции из ContextMenuAttribute ContextMenuAttribute[] ctx = (ContextMenuAttribute[]) meth.GetCustomAttributes(typeof(ContextMenuAttribute), true); MenuCommand callback = new MenuCommand(this, meth); MenuItem item = new MenuItem(ctx[0].Caption, new EventHandler(callback.Execute)); item.DefaultItem = ctx[0].Default; menu.MenuItems.Add(item); } System.Drawing.Point pt = new System.Drawing.Point(x,y); menu.Show(parent, pt); } } private bool Filter(MemberInfo member, object criteria) { bool bInclude = false; // Привести MemberInfo к MethodInfo MethodInfo meth = member as MethodInfo; if (meth != null) { if (meth.ReturnType == typeof(void)) { ParameterInfo[] parms = meth.GetParameters(); if (parms.Length == 0) { // Проверить, есть ли в методе ContextMenuAttribute у метода object[] atts = meth.GetCustomAttributes (typeof(ContextMenuAttribute), true); bInclude = (atts.Length == 1); } } } return bInclude; } }
MemberInfo[] members = this.GetType().FindMembers (MemberTypes.Method, BindingFlags.Public | BindingFlags.Instance, new System.Reflection.MemberFilter(Filter), null);
ContextMenuAttribute[] ctx = (ContextMenuAttribute[]) meth.GetCustomAttributes(typeof(ContextMenuAttribute), true); MenuCommand callback = new MenuCommand(this, meth); MenuItem item = new MenuItem(ctx[0].Caption, new EventHandler(callback.Execute)); item.DefaultItem = ctx[0].Default; menu.MenuItems.Add(item);
object[] atts = meth.GetCustomAttributes (typeof(ContextMenuAttribute), true); bInclude = (atts.Length == 1);
Решение задачи: «.NET 4.x Как оно работает (контекстное меню)?»
textual
Листинг программы
[ContextMenu(Caption="Команда")] public void NameName(object sender, EventArgs e) { ... }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д