• R/O
  • SSH
  • HTTPS

doooblib: Commit


Commit MetaInfo

Revision57 (tree)
Time2014-10-20 07:11:27
Authordoooblib

Log Message

Change Summary

Incremental Difference

--- doooblib/doooblib/Refrection.cs (revision 56)
+++ doooblib/doooblib/Refrection.cs (revision 57)
@@ -2,6 +2,7 @@
22 using System.Collections.Generic;
33 using System.Linq;
44 using System.Text;
5+using System.Reflection;
56
67 namespace dooob
78 {
@@ -13,5 +14,12 @@
1314 Object _object = Activator.CreateInstance(_type);
1415 return _object;
1516 }
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+ }
1624 }
1725 }
--- doooblib/doooblib/_object.cs (nonexistent)
+++ doooblib/doooblib/_object.cs (revision 57)
@@ -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+}
--- doooblib/TestForm/Form1.Designer.cs (revision 56)
+++ doooblib/TestForm/Form1.Designer.cs (revision 57)
@@ -35,6 +35,7 @@
3535 this.button4 = new System.Windows.Forms.Button();
3636 this.button5 = new System.Windows.Forms.Button();
3737 this.button6 = new System.Windows.Forms.Button();
38+ this.button7 = new System.Windows.Forms.Button();
3839 this.SuspendLayout();
3940 //
4041 // button1
@@ -105,11 +106,22 @@
105106 this.button6.UseVisualStyleBackColor = true;
106107 this.button6.Click += new System.EventHandler(this.ビットマップ上に描画スタティッククラス版_Click);
107108 //
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+ //
108119 // Form1
109120 //
110121 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
111122 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);
113125 this.Controls.Add(this.button6);
114126 this.Controls.Add(this.button5);
115127 this.Controls.Add(this.button4);
@@ -133,6 +145,7 @@
133145 private System.Windows.Forms.Button button4;
134146 private System.Windows.Forms.Button button5;
135147 private System.Windows.Forms.Button button6;
148+ private System.Windows.Forms.Button button7;
136149 }
137150 }
138151
--- doooblib/TestForm/Form1.cs (revision 56)
+++ doooblib/TestForm/Form1.cs (revision 57)
@@ -8,7 +8,7 @@
88 using System.Windows.Forms;
99 using system;
1010 using system.drawing;
11-using dooob;
11+using dooob.ExtensionMethod;
1212
1313 namespace TestForm
1414 {
@@ -60,7 +60,22 @@
6060 Bitmap bmp = new Bitmap(100, 40);
6161 __Graphics.DrawString(bmp, "ABC", 0, 0);
6262 }
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+ }
6372 }
73+
74+ class SomeClass
75+ {
76+ public int a { set; get; }
77+ public int b { set; get; }
78+ }
6479 }
6580 #if false
6681
Show on old repository browser