1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; using System.Reflection; namespace インスタンスとクラスでのメンバ { public partial class Form1 : Form { public Form1() { InitializeComponent(); this.Width = 800; this.Height = 600; TextBox tb = new TextBox(); tb.Location = new Point(2, 57); tb.Multiline = true; tb.Size = new System.Drawing.Size(700, 445); tb.Anchor = AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom | AnchorStyles.Left; tb.ScrollBars = ScrollBars.Both; Controls.Add(tb); Button btn = new Button(); btn.Text = "click!"; btn.Location = new Point(2, 2); btn.Size = new System.Drawing.Size(100, 50); Controls.Add(btn); btn.Click += new EventHandler((object sender, EventArgs e)=> { //typeofだと型を直接渡す。静的 //MemberInfo[] mi = typeof(String).GetMembers( //GetTypeだとインスタンスから情報取得可能。動的 System.String s = "Hello World"; MemberInfo[] mi = s.GetType().GetMembers( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy); string buff = ""; foreach (MemberInfo m in mi) { buff = m.Module + ": " + m.ReflectedType + ": " + m.MemberType + ": " + m.Name + "\r\n" + buff; } tb.Text = buff; }); } } } |