回答編集履歴
2
FastMemberの説明を追記
test
CHANGED
@@ -12,6 +12,8 @@
|
|
12
12
|
|
13
13
|
// とりあえず既成の外部クラスを使います。nuget からFastMemberを探して使ってみてください。
|
14
14
|
|
15
|
+
// FastMemberはクラスのアクセサ/ゲッタを生成してキャッシュしてくれるライブラリです。
|
16
|
+
|
15
17
|
IEnumerable<string> AsEnumerable()
|
16
18
|
|
17
19
|
{
|
1
オマケを追加
test
CHANGED
@@ -147,3 +147,249 @@
|
|
147
147
|
}
|
148
148
|
|
149
149
|
```
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
---
|
158
|
+
|
159
|
+
追記
|
160
|
+
|
161
|
+
オマケ。コピペで使える全文です。
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
リフレクション
|
166
|
+
|
167
|
+
```C#
|
168
|
+
|
169
|
+
using FastMember;
|
170
|
+
|
171
|
+
using System;
|
172
|
+
|
173
|
+
using System.Collections.Generic;
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
class Program
|
178
|
+
|
179
|
+
{
|
180
|
+
|
181
|
+
static void Main(string[] args)
|
182
|
+
|
183
|
+
{
|
184
|
+
|
185
|
+
var sample = new ReflectionSample();
|
186
|
+
|
187
|
+
foreach (var str in sample.AsEnumerable())
|
188
|
+
|
189
|
+
{
|
190
|
+
|
191
|
+
Console.WriteLine(str);
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
Console.ReadKey();
|
198
|
+
|
199
|
+
}
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
public class ReflectionSample
|
206
|
+
|
207
|
+
{
|
208
|
+
|
209
|
+
public string a1 = "AAA";
|
210
|
+
|
211
|
+
public string a2 = "BBB";
|
212
|
+
|
213
|
+
public string a3 = "CCC";
|
214
|
+
|
215
|
+
}
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
public static class ReflectionSampleExtension
|
220
|
+
|
221
|
+
{
|
222
|
+
|
223
|
+
public static IEnumerable<string> AsEnumerable(this ReflectionSample obj)
|
224
|
+
|
225
|
+
{
|
226
|
+
|
227
|
+
var accessor = TypeAccessor.Create(typeof(ReflectionSample));
|
228
|
+
|
229
|
+
for (int i = 1; i < 3; ++i)
|
230
|
+
|
231
|
+
{
|
232
|
+
|
233
|
+
yield return (string)accessor[obj, $"a{i}"];
|
234
|
+
|
235
|
+
}
|
236
|
+
|
237
|
+
}
|
238
|
+
|
239
|
+
}
|
240
|
+
|
241
|
+
```
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
直書き
|
246
|
+
|
247
|
+
```C#
|
248
|
+
|
249
|
+
using System;
|
250
|
+
|
251
|
+
using System.Collections.Generic;
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
class Program
|
256
|
+
|
257
|
+
{
|
258
|
+
|
259
|
+
static void Main(string[] args)
|
260
|
+
|
261
|
+
{
|
262
|
+
|
263
|
+
var sample = new Sample();
|
264
|
+
|
265
|
+
foreach (var str in sample.AsEnumerable愚直1())
|
266
|
+
|
267
|
+
{
|
268
|
+
|
269
|
+
Console.WriteLine(str);
|
270
|
+
|
271
|
+
}
|
272
|
+
|
273
|
+
foreach (var str in sample.AsEnumerable愚直2())
|
274
|
+
|
275
|
+
{
|
276
|
+
|
277
|
+
Console.WriteLine(str);
|
278
|
+
|
279
|
+
}
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
Console.ReadKey();
|
284
|
+
|
285
|
+
}
|
286
|
+
|
287
|
+
}
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
public class Sample
|
292
|
+
|
293
|
+
{
|
294
|
+
|
295
|
+
public string a1 = "AAA";
|
296
|
+
|
297
|
+
public string a2 = "BBB";
|
298
|
+
|
299
|
+
public string a3 = "CCC";
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
// 愚直1
|
304
|
+
|
305
|
+
public IEnumerable<string> AsEnumerable愚直1()
|
306
|
+
|
307
|
+
{
|
308
|
+
|
309
|
+
yield return a1;
|
310
|
+
|
311
|
+
yield return a2;
|
312
|
+
|
313
|
+
yield return a3;
|
314
|
+
|
315
|
+
}
|
316
|
+
|
317
|
+
|
318
|
+
|
319
|
+
public string this[int index]
|
320
|
+
|
321
|
+
{
|
322
|
+
|
323
|
+
get
|
324
|
+
|
325
|
+
{
|
326
|
+
|
327
|
+
switch (index)
|
328
|
+
|
329
|
+
{
|
330
|
+
|
331
|
+
case 0: return a1;
|
332
|
+
|
333
|
+
case 1: return a2;
|
334
|
+
|
335
|
+
case 2: return a3;
|
336
|
+
|
337
|
+
// ....
|
338
|
+
|
339
|
+
// case 98: return a99;
|
340
|
+
|
341
|
+
default:
|
342
|
+
|
343
|
+
throw new IndexOutOfRangeException();
|
344
|
+
|
345
|
+
}
|
346
|
+
|
347
|
+
}
|
348
|
+
|
349
|
+
set
|
350
|
+
|
351
|
+
{
|
352
|
+
|
353
|
+
switch (index)
|
354
|
+
|
355
|
+
{
|
356
|
+
|
357
|
+
case 0: a1 = value; break;
|
358
|
+
|
359
|
+
case 1: a2 = value; break;
|
360
|
+
|
361
|
+
case 2: a3 = value; break;
|
362
|
+
|
363
|
+
// ....
|
364
|
+
|
365
|
+
// case 98: a99 = value; break;
|
366
|
+
|
367
|
+
default:
|
368
|
+
|
369
|
+
throw new IndexOutOfRangeException();
|
370
|
+
|
371
|
+
}
|
372
|
+
|
373
|
+
}
|
374
|
+
|
375
|
+
}
|
376
|
+
|
377
|
+
|
378
|
+
|
379
|
+
public IEnumerable<string> AsEnumerable愚直2()
|
380
|
+
|
381
|
+
{
|
382
|
+
|
383
|
+
for (int i = 0; i < 3; ++i)
|
384
|
+
|
385
|
+
{
|
386
|
+
|
387
|
+
yield return this[i];
|
388
|
+
|
389
|
+
}
|
390
|
+
|
391
|
+
}
|
392
|
+
|
393
|
+
}
|
394
|
+
|
395
|
+
```
|