teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

FastMemberの説明を追記

2017/01/27 02:58

投稿

haru666
haru666

スコア1593

answer CHANGED
@@ -5,6 +5,7 @@
5
5
  // 今回はリフレクションにFastMemberを使います。
6
6
  // リフレクションは遅いため、何等かの方法でキャッシュしておくことが望ましいです
7
7
  // とりあえず既成の外部クラスを使います。nuget からFastMemberを探して使ってみてください。
8
+ // FastMemberはクラスのアクセサ/ゲッタを生成してキャッシュしてくれるライブラリです。
8
9
  IEnumerable<string> AsEnumerable()
9
10
  {
10
11
  var accessor = TypeAccessor.Create(typeof(/*型*/));

1

オマケを追加

2017/01/27 02:58

投稿

haru666
haru666

スコア1593

answer CHANGED
@@ -72,4 +72,127 @@
72
72
  }
73
73
  }
74
74
  }
75
+ ```
76
+
77
+
78
+
79
+ ---
80
+ 追記
81
+ オマケ。コピペで使える全文です。
82
+
83
+ リフレクション
84
+ ```C#
85
+ using FastMember;
86
+ using System;
87
+ using System.Collections.Generic;
88
+
89
+ class Program
90
+ {
91
+ static void Main(string[] args)
92
+ {
93
+ var sample = new ReflectionSample();
94
+ foreach (var str in sample.AsEnumerable())
95
+ {
96
+ Console.WriteLine(str);
97
+ }
98
+
99
+ Console.ReadKey();
100
+ }
101
+ }
102
+
103
+ public class ReflectionSample
104
+ {
105
+ public string a1 = "AAA";
106
+ public string a2 = "BBB";
107
+ public string a3 = "CCC";
108
+ }
109
+
110
+ public static class ReflectionSampleExtension
111
+ {
112
+ public static IEnumerable<string> AsEnumerable(this ReflectionSample obj)
113
+ {
114
+ var accessor = TypeAccessor.Create(typeof(ReflectionSample));
115
+ for (int i = 1; i < 3; ++i)
116
+ {
117
+ yield return (string)accessor[obj, $"a{i}"];
118
+ }
119
+ }
120
+ }
121
+ ```
122
+
123
+ 直書き
124
+ ```C#
125
+ using System;
126
+ using System.Collections.Generic;
127
+
128
+ class Program
129
+ {
130
+ static void Main(string[] args)
131
+ {
132
+ var sample = new Sample();
133
+ foreach (var str in sample.AsEnumerable愚直1())
134
+ {
135
+ Console.WriteLine(str);
136
+ }
137
+ foreach (var str in sample.AsEnumerable愚直2())
138
+ {
139
+ Console.WriteLine(str);
140
+ }
141
+
142
+ Console.ReadKey();
143
+ }
144
+ }
145
+
146
+ public class Sample
147
+ {
148
+ public string a1 = "AAA";
149
+ public string a2 = "BBB";
150
+ public string a3 = "CCC";
151
+
152
+ // 愚直1
153
+ public IEnumerable<string> AsEnumerable愚直1()
154
+ {
155
+ yield return a1;
156
+ yield return a2;
157
+ yield return a3;
158
+ }
159
+
160
+ public string this[int index]
161
+ {
162
+ get
163
+ {
164
+ switch (index)
165
+ {
166
+ case 0: return a1;
167
+ case 1: return a2;
168
+ case 2: return a3;
169
+ // ....
170
+ // case 98: return a99;
171
+ default:
172
+ throw new IndexOutOfRangeException();
173
+ }
174
+ }
175
+ set
176
+ {
177
+ switch (index)
178
+ {
179
+ case 0: a1 = value; break;
180
+ case 1: a2 = value; break;
181
+ case 2: a3 = value; break;
182
+ // ....
183
+ // case 98: a99 = value; break;
184
+ default:
185
+ throw new IndexOutOfRangeException();
186
+ }
187
+ }
188
+ }
189
+
190
+ public IEnumerable<string> AsEnumerable愚直2()
191
+ {
192
+ for (int i = 0; i < 3; ++i)
193
+ {
194
+ yield return this[i];
195
+ }
196
+ }
197
+ }
75
198
  ```