http://kmlinux.fjfi.cvut.cz/~culikzde/pw/Components03.zip using System; using System.Collections.Generic; // using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Reflection; using System.ComponentModel; namespace Components { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void RunButton_Click(object sender, EventArgs e) { displayTree(this); } [Description("Zobraz strom")] public void displayTree (Control obj) { tree.Nodes.Clear (); displayBranch (tree.Nodes, obj); } private void displayBranch (TreeNodeCollection branch, Control obj) { TreeNode item = new TreeNode (); item.Text = obj.ToString(); item.Tag = obj; branch.Add(item); foreach (Control c in obj.Controls) displayBranch (item.Nodes, c); } // using System.ComponentModel; [Description("Pozdrav")] public string hello () { return "Ahoj"; } [Description("Secti dve cisla")] public int add ( int a, int b) { return a+b; } // using System.ComponentModel; [Description ("Vytiskni radku") ] public void put (string s) { info.AppendText(s + "\r\n"); } private void Tree_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { object obj = e.Node.Tag; prop.SelectedObject = obj; info.Text = ""; Type t = obj.GetType(); put("Type " + t.FullName); MethodInfo[] mm = t.GetMethods(); foreach (MethodInfo m in mm) { DescriptionAttribute a = m.GetCustomAttribute (); if (a != null) { string s = ""; ParameterInfo[] args = m.GetParameters(); if (args.Length == 0) { object [] param = { }; object result = m.Invoke (obj, param); s = result.ToString(); } else if (args.Length == 2) { ParameterInfo p0 = args[0]; ParameterInfo p1 = args[1]; if (p0.ParameterType == typeof(Int32) && p1.ParameterType == typeof(Int32)) { object[] param = { 1, 7 }; object result = m.Invoke(obj, param); s = "(" + p0.Name + "=" + param[0].ToString() + ","; s = s + p1.Name + "=" + param[1].ToString() + ")"; s = s + " -> " + result.ToString(); } } put("Method " + m.Name + " ... " + a.Description + " ... " + s); } } // using System.Reflection; /* PropertyInfo[] p = t.GetProperties(); foreach (PropertyInfo v in p) { object value = v.GetValue(obj); string text = ""; if (value != null) text = value.ToString(); put("Property " + v.Name + " = " + text); } */ /* foreach (CustomAttributeData a in v.CustomAttributes) { put("Attribute " + v.Name + " . " + a.ToString ()); } */ } private TreeNode currentNode = null; private void Tree_MouseClick(object sender, MouseEventArgs e) { currentNode = tree.GetNodeAt(e.X, e.Y); } private void ContextMenu_Opening(object sender, CancelEventArgs e) { contextMenu.Items.Clear(); if (currentNode != null) { object obj = currentNode.Tag; contextMenu.Items.Add("opening " + obj.ToString()); Type t = obj.GetType(); MethodInfo[] methods = t.GetMethods(); foreach (MethodInfo m in methods) { DescriptionAttribute a = m.GetCustomAttribute(); if (a != null) { string s = ""; ParameterInfo[] args = m.GetParameters(); if (args.Length == 0) { MyMenuItem item = new MyMenuItem (); item.Text = m.Name + " ... " + a.Description; item.obj = obj; item.m = m; item.Click += ContextMenuClick; contextMenu.Items.Add(item); } } } } } private void ContextMenuClick(object sender, EventArgs e) { MyMenuItem item = sender as MyMenuItem; object result = item.m.Invoke(item.obj, new object[] { }); put("MENU CLICK ... " + result.ToString()); } } class MyMenuItem : ToolStripMenuItem { public object obj; public MethodInfo m; }; }