| @@ -2,6 +2,7 @@ | ||
| 2 | 2 | using System.Collections.Generic; |
| 3 | 3 | using System.Linq; |
| 4 | 4 | using System.Text; |
| 5 | +using System.Reflection; | |
| 5 | 6 | |
| 6 | 7 | namespace dooob |
| 7 | 8 | { |
| @@ -13,5 +14,12 @@ | ||
| 13 | 14 | Object _object = Activator.CreateInstance(_type); |
| 14 | 15 | return _object; |
| 15 | 16 | } |
| 17 | + | |
| 18 | + public static void SetPropertyByName<T>(T obj, string name, string value) | |
| 19 | + { | |
| 20 | + Type t = obj.GetType(); | |
| 21 | + PropertyInfo proInfo = obj.GetType().GetProperty(name); | |
| 22 | + proInfo.SetValue(obj, value, null); | |
| 23 | + } | |
| 16 | 24 | } |
| 17 | 25 | } |
| @@ -0,0 +1,50 @@ | ||
| 1 | +using System; | |
| 2 | +using System.Text; | |
| 3 | +using System.Reflection; | |
| 4 | + | |
| 5 | +namespace dooob.ExtensionMethod | |
| 6 | +{ | |
| 7 | + /// <summary> | |
| 8 | + /// オブジェクトに関する拡張メソッド | |
| 9 | + /// </summary> | |
| 10 | + public static class _object | |
| 11 | + { | |
| 12 | + /// <summary> | |
| 13 | + /// クラスの全てのプロパティ値を文字列で返す。文字列フォーマットは以下の通り。 | |
| 14 | + /// <para>PropertyName1:value1[ret]</para> | |
| 15 | + /// <para>PropertyName2:value2[ret]</para> | |
| 16 | + /// </summary> | |
| 17 | + /// <typeparam name="T"></typeparam> | |
| 18 | + /// <param name="obj"></param> | |
| 19 | + /// <returns></returns> | |
| 20 | + public static string ToString<T>(this T obj) | |
| 21 | + { | |
| 22 | + Type t = obj.GetType(); | |
| 23 | + | |
| 24 | + MemberInfo[] memberList = t.GetMembers( | |
| 25 | + BindingFlags.Public | | |
| 26 | + BindingFlags.Instance | |
| 27 | + ); | |
| 28 | + | |
| 29 | + StringBuilder stringData = new StringBuilder(); | |
| 30 | + | |
| 31 | + foreach (MemberInfo memberInfo in memberList) | |
| 32 | + { | |
| 33 | + // プロパティか? | |
| 34 | + if (memberInfo.MemberType == MemberTypes.Property) | |
| 35 | + { | |
| 36 | + // プロパティ名を特定する | |
| 37 | + string prpertyName = memberInfo.Name; | |
| 38 | + | |
| 39 | + // プロパティ名からプロパティ情報を取得 | |
| 40 | + PropertyInfo proInfo = obj.GetType().GetProperty(memberInfo.Name); | |
| 41 | + string propertyValue = proInfo.GetValue(obj, null).ToString(); | |
| 42 | + | |
| 43 | + stringData.Append(prpertyName + ":" + propertyValue + "\r\n"); | |
| 44 | + } | |
| 45 | + } | |
| 46 | + | |
| 47 | + return stringData.ToString(); | |
| 48 | + } | |
| 49 | + } | |
| 50 | +} |
| @@ -35,6 +35,7 @@ | ||
| 35 | 35 | this.button4 = new System.Windows.Forms.Button(); |
| 36 | 36 | this.button5 = new System.Windows.Forms.Button(); |
| 37 | 37 | this.button6 = new System.Windows.Forms.Button(); |
| 38 | + this.button7 = new System.Windows.Forms.Button(); | |
| 38 | 39 | this.SuspendLayout(); |
| 39 | 40 | // |
| 40 | 41 | // button1 |
| @@ -105,11 +106,22 @@ | ||
| 105 | 106 | this.button6.UseVisualStyleBackColor = true; |
| 106 | 107 | this.button6.Click += new System.EventHandler(this.ビットマップ上に描画スタティッククラス版_Click); |
| 107 | 108 | // |
| 109 | + // button7 | |
| 110 | + // | |
| 111 | + this.button7.Location = new System.Drawing.Point(12, 253); | |
| 112 | + this.button7.Name = "button7"; | |
| 113 | + this.button7.Size = new System.Drawing.Size(67, 33); | |
| 114 | + this.button7.TabIndex = 7; | |
| 115 | + this.button7.Text = "button7"; | |
| 116 | + this.button7.UseVisualStyleBackColor = true; | |
| 117 | + this.button7.Click += new System.EventHandler(this.button7_Click); | |
| 118 | + // | |
| 108 | 119 | // Form1 |
| 109 | 120 | // |
| 110 | 121 | this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); |
| 111 | 122 | this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; |
| 112 | - this.ClientSize = new System.Drawing.Size(535, 262); | |
| 123 | + this.ClientSize = new System.Drawing.Size(535, 345); | |
| 124 | + this.Controls.Add(this.button7); | |
| 113 | 125 | this.Controls.Add(this.button6); |
| 114 | 126 | this.Controls.Add(this.button5); |
| 115 | 127 | this.Controls.Add(this.button4); |
| @@ -133,6 +145,7 @@ | ||
| 133 | 145 | private System.Windows.Forms.Button button4; |
| 134 | 146 | private System.Windows.Forms.Button button5; |
| 135 | 147 | private System.Windows.Forms.Button button6; |
| 148 | + private System.Windows.Forms.Button button7; | |
| 136 | 149 | } |
| 137 | 150 | } |
| 138 | 151 |
| @@ -8,7 +8,7 @@ | ||
| 8 | 8 | using System.Windows.Forms; |
| 9 | 9 | using system; |
| 10 | 10 | using system.drawing; |
| 11 | -using dooob; | |
| 11 | +using dooob.ExtensionMethod; | |
| 12 | 12 | |
| 13 | 13 | namespace TestForm |
| 14 | 14 | { |
| @@ -60,7 +60,22 @@ | ||
| 60 | 60 | Bitmap bmp = new Bitmap(100, 40); |
| 61 | 61 | __Graphics.DrawString(bmp, "ABC", 0, 0); |
| 62 | 62 | } |
| 63 | + | |
| 64 | + private void button7_Click(object sender, EventArgs e) | |
| 65 | + { | |
| 66 | + SomeClass someClass = new SomeClass() { a = 3, b = 5 }; | |
| 67 | + string str = someClass.ToString<SomeClass>(); | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + } | |
| 63 | 72 | } |
| 73 | + | |
| 74 | + class SomeClass | |
| 75 | + { | |
| 76 | + public int a { set; get; } | |
| 77 | + public int b { set; get; } | |
| 78 | + } | |
| 64 | 79 | } |
| 65 | 80 | #if false |
| 66 | 81 |