forked from buunguyen/fasterflect
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathReflect.cs
649 lines (600 loc) · 33.5 KB
/
Reflect.cs
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
#region License
// Copyright © 2010 Buu Nguyen, Morten Mertner
// Copyright © 2018 Wesley Hamilton
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// The latest version of this file can be found at https://github.com/ffhighwind/fasterflect
#endregion
using Fasterflect.Emitter;
using System;
using System.Reflection;
using System.Collections.Generic;
namespace Fasterflect
{
/// <summary>
/// Helper class for producing Reflection-based delegates and other utility methods.
/// </summary>
public static class Reflect
{
#region Cache
private static readonly Cache<MemberCallInfo, MemberGetter> Getters = new Cache<MemberCallInfo, MemberGetter>();
private static readonly Cache<MemberCallInfo, MemberSetter> Setters = new Cache<MemberCallInfo, MemberSetter>();
private static readonly Cache<CtorInfo, ConstructorInvoker> Constructors = new Cache<CtorInfo, ConstructorInvoker>();
private static readonly Cache<CallInfo, MethodInvoker> Methods = new Cache<CallInfo, MethodInvoker>();
private static readonly Cache<Type, ArrayElementSetter> ArraySetters = new Cache<Type, ArrayElementSetter>();
private static readonly Cache<Type, ArrayElementGetter> ArrayGetters = new Cache<Type, ArrayElementGetter>();
private static readonly Cache<MultiSetCallInfo, MultiSetter> MultiSetters = new Cache<MultiSetCallInfo, MultiSetter>();
private static readonly Cache<MapCallInfo, ObjectMapper> Mappers = new Cache<MapCallInfo, ObjectMapper>();
#endregion Cache
#region Constructor
internal static ConstructorInvoker Constructor(Type type, BindingFlags bindingFlags, ConstructorInfo ctor, Type[] parameterTypes)
{
bindingFlags &= FasterflectFlags.InstanceAnyDeclaredOnly;
CtorInfo info = new CtorInfo(type, bindingFlags, parameterTypes);
ConstructorInvoker value = Constructors.Get(info);
if (value != null)
return value;
if (ctor == null && (!type.IsValueType || parameterTypes.Length != 0)) {
ctor = ReflectLookup.Constructor(type, bindingFlags, parameterTypes) ?? throw new MissingMemberException(type.FullName, "ctor()");
}
value = (ConstructorInvoker)new CtorInvocationEmitter(type, ctor).GetDelegate();
Constructors.Insert(info, value);
return value;
}
/// <summary>
/// Creates a <see cref="ConstructorInvoker"/> which invokes the given <see cref="ConstructorInfo"/>.
/// </summary>
/// <param name="type">The <see cref="Type"/> that the <see cref="ConstructorInvoker"/> will construct.</param>
/// <param name="parameterTypes">The <see cref="ConstructorInfo"/>'s parameters types.</param>
/// <returns>A <see cref="ConstructorInvoker"/> which invokes the given <see cref="ConstructorInfo"/>.</returns>
public static ConstructorInvoker Constructor(Type type, params Type[] parameterTypes)
{
return Constructor(type, FasterflectFlags.InstanceAnyDeclaredOnly, null, parameterTypes);
}
/// <summary>
/// Creates a <see cref="ConstructorInvoker"/> which invokes the given <see cref="ConstructorInfo"/>.
/// </summary>
/// <param name="type">The <see cref="Type"/> that the <see cref="ConstructorInvoker"/> will construct.</param>
/// <param name="bindingFlags">The <see cref="BindingFlags"/> or <see cref="FasterflectFlags"/> to use when searching for the <see cref="ConstructorInfo"/>.</param>
/// <param name="parameterTypes">The <see cref="ConstructorInfo"/>'s parameters types.</param>
/// <returns>A <see cref="ConstructorInvoker"/> which invokes the given <see cref="ConstructorInfo"/>.</returns>
public static ConstructorInvoker Constructor(Type type, BindingFlags bindingFlags, params Type[] parameterTypes)
{
return Constructor(type, bindingFlags, null, parameterTypes);
}
/// <summary>
/// Creates a <see cref="ConstructorInvoker"/> which invokes the given <see cref="ConstructorInfo"/>.
/// </summary>
/// <param name="ctor">The <see cref="ConstructorInfo"/> that the <see cref="ConstructorInvoker"/> will invoke.</param>
/// <returns>A <see cref="ConstructorInvoker"/> which invokes the given <see cref="ConstructorInfo"/>.</returns>
public static ConstructorInvoker Constructor(ConstructorInfo ctor)
{
return Constructor(ctor.DeclaringType, FasterflectFlags.InstanceAnyDeclaredOnly, ctor, ctor.GetParameters().ToTypeArray());
}
#endregion
#region Getters
internal static MemberGetter Getter(Type type, string name, MemberTypes memberType, FasterflectFlags bindingFlags, MemberInfo memberInfo)
{
MemberCallInfo info = new MemberCallInfo(type, name, memberType, bindingFlags);
MemberGetter value = Getters.Get(info);
if (value != null)
return value;
memberInfo = memberInfo ?? ReflectLookup.Member(info.TargetType, info.MemberName, info.BindingFlags, memberType);
if (memberInfo == null) {
if(memberType == MemberTypes.Field)
throw new MissingFieldException(info.TargetType.FullName, info.MemberName);
throw new MissingMemberException(info.TargetType.FullName, info.MemberName);
}
info.MemberType = memberInfo.MemberType;
value = (MemberGetter)new MemberGetEmitter(memberInfo).GetDelegate();
Getters.Insert(info, value);
return value;
}
/// <summary>
/// Creates a <see cref="MemberGetter"/> which gets the value of the given member.
/// </summary>
/// <param name="type">The <see cref="Type"/> of the member.</param>
/// <param name="name">The name of the member.</param>
/// <returns>A <see cref="MemberGetter"/> which gets the value of the given member.</returns>
public static MemberGetter Getter(Type type, string name)
{
return Getter(type, name, MemberTypes.Field | MemberTypes.Property, FasterflectFlags.StaticInstanceAnyVisibility, null);
}
/// <summary>
/// Creates a <see cref="MemberGetter"/> which gets the value of the given member.
/// </summary>
/// <param name="type">The <see cref="Type"/> of the member.</param>
/// <param name="name">The name of the member.</param>
/// <param name="bindingFlags">The <see cref="BindingFlags"/> or <see cref="FasterflectFlags"/> to filter the member.</param>
/// <returns>A <see cref="MemberGetter"/> which gets the value of the given member.</returns>
public static MemberGetter Getter(Type type, string name, FasterflectFlags bindingFlags)
{
return Getter(type, name, MemberTypes.Field | MemberTypes.Property, bindingFlags, null);
}
/// <summary>
/// Creates a <see cref="MemberGetter"/> which gets the value of the given member.
/// </summary>
/// <param name="memberInfo">The <see cref="MemberInfo"/> whos value will be returned.</param>
/// <returns>A <see cref="MemberGetter"/> which gets the value of the given member.</returns>
public static MemberGetter Getter(MemberInfo memberInfo)
{
return Getter(memberInfo.DeclaringType, memberInfo.Name, MemberTypes.Field | MemberTypes.Property, FasterflectFlags.StaticInstanceAnyVisibility, memberInfo);
}
/// <summary>
/// Creates a <see cref="MemberGetter"/> which gets the value of the given <see cref="FieldInfo"/>.
/// </summary>
/// <param name="field">The <see cref="FieldInfo"/> whose value will be returned.</param>
/// <returns>A <see cref="MemberGetter"/> which gets the value of the given <see cref="FieldInfo"/>.</returns>
public static MemberGetter FieldGetter(FieldInfo field)
{
return Getter(field.DeclaringType, field.Name, MemberTypes.Field, FasterflectFlags.StaticInstanceAnyVisibility, field);
}
/// <summary>
/// Creates a <see cref="MemberGetter"/> which gets the value of the given <see cref="FieldInfo"/>.
/// </summary>
/// <param name="type">The <see cref="Type"/> of the <see cref="FieldInfo"/>.</param>
/// <param name="name">The name of the <see cref="FieldInfo"/>.</param>
/// <returns>A <see cref="MemberGetter"/> which gets the value of the given <see cref="FieldInfo"/>.</returns>
public static MemberGetter FieldGetter(Type type, string name)
{
return Getter(type, name, MemberTypes.Field, FasterflectFlags.StaticInstanceAnyVisibility, null);
}
/// <summary>
/// Creates a <see cref="MemberGetter"/> which gets the value of the given <see cref="FieldInfo"/>.
/// </summary>
/// <param name="type">The <see cref="Type"/> of the <see cref="FieldInfo"/>.</param>
/// <param name="name">The name of the <see cref="FieldInfo"/>.</param>
/// <param name="bindingFlags">The <see cref="BindingFlags"/> or <see cref="FasterflectFlags"/> to filter the <see cref="FieldInfo"/>.</param>
/// <returns>A <see cref="MemberGetter"/> which gets the value of the given <see cref="FieldInfo"/>.</returns>
public static MemberGetter FieldGetter(Type type, string name, FasterflectFlags bindingFlags)
{
return Getter(type, name, MemberTypes.Field, bindingFlags, null);
}
/// <summary>
/// Creates a <see cref="MemberGetter"/> which gets the value of the given <see cref="PropertyInfo"/>.
/// </summary>
/// <param name="property">The <see cref="PropertyInfo"/> whose value will be returned.</param>
/// <returns>A <see cref="MemberGetter"/> which gets the value of the given <see cref="PropertyInfo"/>.</returns>
public static MemberGetter PropertyGetter(PropertyInfo property)
{
return Getter(property.DeclaringType, property.Name, MemberTypes.Property, FasterflectFlags.StaticInstanceAnyVisibility, property);
}
/// <summary>
/// Creates a <see cref="MemberGetter"/> which gets the value of the given <see cref="PropertyInfo"/>.
/// </summary>
/// <param name="type">The <see cref="Type"/> of the <see cref="PropertyInfo"/>.</param>
/// <param name="name">The name of the <see cref="PropertyInfo"/>.</param>
/// <returns>A <see cref="MemberGetter"/> which gets the value of the given <see cref="PropertyInfo"/>.</returns>
public static MemberGetter PropertyGetter(Type type, string name)
{
return Getter(type, name, MemberTypes.Property, FasterflectFlags.StaticInstanceAnyVisibility, null);
}
/// <summary>
/// Creates a <see cref="MemberGetter"/> which gets the value of the given <see cref="PropertyInfo"/>.
/// </summary>
/// <param name="type">The <see cref="Type"/> of the <see cref="PropertyInfo"/>.</param>
/// <param name="name">The name of the <see cref="PropertyInfo"/>.</param>
/// <param name="bindingFlags">The <see cref="BindingFlags"/> or <see cref="FasterflectFlags"/> to filter the <see cref="PropertyInfo"/>.</param>
/// <returns>A <see cref="MemberGetter"/> which gets the value of the given <see cref="PropertyInfo"/>.</returns>
public static MemberGetter PropertyGetter(Type type, string name, FasterflectFlags bindingFlags)
{
return Getter(type, name, MemberTypes.Property, bindingFlags, null);
}
#endregion
#region Setters
internal static MemberSetter Setter(Type type, string name, MemberTypes memberType, FasterflectFlags bindingFlags, MemberInfo memberInfo)
{
MemberCallInfo info = new MemberCallInfo(type, name, memberType, bindingFlags);
MemberSetter value = Setters.Get(info);
if (value != null)
return value;
memberInfo = memberInfo ?? ReflectLookup.Member(info.TargetType, info.MemberName, info.BindingFlags);
if (memberInfo == null) {
if (memberType == MemberTypes.Field)
throw new MissingFieldException(info.TargetType.FullName, info.MemberName);
throw new MissingMemberException(info.TargetType.FullName, info.MemberName);
}
info.MemberType = memberInfo.MemberType;
value = (MemberSetter)new MemberSetEmitter(memberInfo).GetDelegate();
Setters.Insert(info, value);
return value;
}
/// <summary>
/// Creates a <see cref="MemberSetter"/> which sets the value of the given member.
/// </summary>
/// <param name="memberInfo">The <see cref="MemberInfo"/> whos value will be set.</param>
/// <returns>A <see cref="MemberSetter"/> which sets the value of the given member.</returns>
public static MemberSetter Setter(MemberInfo memberInfo)
{
return Setter(memberInfo.DeclaringType, memberInfo.Name, MemberTypes.Field | MemberTypes.Property, FasterflectFlags.StaticInstanceAnyVisibility, null);
}
/// <summary>
/// Creates a <see cref="MemberSetter"/> which sets the value of the given member.
/// </summary>
/// <param name="type">The <see cref="Type"/> whose member will be set.</param>
/// <param name="name">The name of the member to set.</param>
/// <returns>A <see cref="MemberSetter"/> which sets the value of the given member.</returns>
public static MemberSetter Setter(Type type, string name)
{
return Setter(type, name, MemberTypes.Field | MemberTypes.Property, FasterflectFlags.StaticInstanceAnyVisibility, null);
}
/// <summary>
/// Creates a <see cref="MemberSetter"/> which sets the value of the given member.
/// </summary>
/// <param name="type">The <see cref="Type"/> whose member will be set.</param>
/// <param name="name">The name of the member to set.</param>
/// <param name="bindingFlags">The <see cref="BindingFlags"/> or <see cref="FasterflectFlags"/> to filter the member.</param>
/// <returns>A <see cref="MemberSetter"/> which sets the value of the given member.</returns>
public static MemberSetter Setter(Type type, string name, FasterflectFlags bindingFlags)
{
return Setter(type, name, MemberTypes.Field | MemberTypes.Property, bindingFlags, null);
}
/// <summary>
/// Creates a <see cref="MemberSetter"/> which sets the value of the given <see cref="FieldInfo"/>.
/// </summary>
/// <param name="field">The <see cref="FieldInfo"/> whose value will be set.</param>
/// <returns>A <see cref="MemberSetter"/> which sets the value of the given <see cref="FieldInfo"/>.</returns>
public static MemberSetter FieldSetter(FieldInfo field)
{
return Setter(field.DeclaringType, field.Name, MemberTypes.Field, FasterflectFlags.StaticInstanceAnyVisibility, field);
}
/// <summary>
/// Creates a <see cref="MemberSetter"/> which sets the value of the given <see cref="PropertyInfo"/>.
/// </summary>
/// <param name="property">The <see cref="PropertyInfo"/> whose value will be set.</param>
/// <returns>A <see cref="MemberSetter"/> which sets the value of the given <see cref="PropertyInfo"/>.</returns>
public static MemberSetter PropertySetter(PropertyInfo property)
{
return Setter(property.DeclaringType, property.Name, MemberTypes.Property, FasterflectFlags.StaticInstanceAnyVisibility, property);
}
/// <summary>
/// Creates a <see cref="MemberSetter"/> which sets the value of the given <see cref="PropertyInfo"/>.
/// </summary>
/// <param name="type">The <see cref="Type"/> whose member will be set.</param>
/// <param name="name">The name of the member to set.</param>
/// <returns>A <see cref="MemberSetter"/> which sets the value of the given <see cref="PropertyInfo"/>.</returns>
public static MemberSetter PropertySetter(Type type, string name)
{
return Setter(type, name, MemberTypes.Property, FasterflectFlags.StaticInstanceAnyVisibility, null);
}
/// <summary>
/// Creates a <see cref="MemberSetter"/> which sets the value of the given <see cref="PropertyInfo"/>.
/// </summary>
/// <param name="type">The <see cref="Type"/> whose <see cref="PropertyInfo"/> will be set.</param>
/// <param name="name">The name of the <see cref="PropertyInfo"/> to set.</param>
/// <param name="bindingFlags">The <see cref="BindingFlags"/> or <see cref="FasterflectFlags"/> to filter the member.</param>
/// <returns>A<see cref="MemberSetter"/> which sets the value of the given <see cref="PropertyInfo"/>.</returns>
public static MemberSetter PropertySetter(Type type, string name, FasterflectFlags bindingFlags)
{
return Setter(type, name, MemberTypes.Property, bindingFlags, null);
}
/// <summary>
/// Creates a <see cref="MemberSetter"/> which sets the value of the given <see cref="FieldInfo"/>.
/// </summary>
/// <param name="type">The <see cref="FieldInfo"/> whose value will be set.</param>
/// <param name="name">The name of the <see cref="FieldInfo"/> to set.</param>
/// <returns>A <see cref="MemberSetter"/> which sets the value of the given <see cref="FieldInfo"/>.</returns>
public static MemberSetter FieldSetter(Type type, string name)
{
return Setter(type, name, MemberTypes.Field, FasterflectFlags.StaticInstanceAnyVisibility, null);
}
/// <summary>
/// Creates a <see cref="MemberSetter"/> which sets the value of the given <see cref="FieldInfo"/>.
/// </summary>
/// <param name="type">The <see cref="FieldInfo"/> whose value will be set.</param>
/// <param name="name">The name of the <see cref="FieldInfo"/> to set.</param>
/// <param name="bindingFlags">The <see cref="BindingFlags"/> or <see cref="FasterflectFlags"/> to filter the <see cref="FieldInfo"/>.</param>
/// <returns>A <see cref="MemberSetter"/> which sets the value of the given <see cref="FieldInfo"/>.</returns>
public static MemberSetter FieldSetter(Type type, string name, FasterflectFlags bindingFlags)
{
return Setter(type, name, MemberTypes.Field, bindingFlags, null);
}
#endregion
#region MultiSetter
/// <summary>
/// Creates a <see cref="Fasterflect.MultiSetter"/> which sets the values of the given members.
/// </summary>
/// <param name="type">The <see cref="Type"/> whose members will be set.</param>
/// <param name="memberNames">The names of the members to set.</param>
/// <returns>A <see cref="Fasterflect.MultiSetter"/> which sets the values of the given members.</returns>
public static MultiSetter MultiSetter(Type type, params string[] memberNames)
{
return MultiSetter(type, FasterflectFlags.StaticInstanceAnyVisibility | BindingFlags.SetProperty | BindingFlags.SetField, memberNames);
}
/// <summary>
/// Creates a <see cref="Fasterflect.MultiSetter"/> which sets the values of the given members.
/// </summary>
/// <param name="type">The <see cref="Type"/> whose members will be set.</param>
/// <param name="bindingFlags">The <see cref="BindingFlags"/> or <see cref="FasterflectFlags"/> to filter the members.</param>
/// <param name="memberNames">The names of the members to set.</param>
/// <returns>A <see cref="Fasterflect.MultiSetter"/> which sets the values of the given members.</returns>
public static MultiSetter MultiSetter(Type type, FasterflectFlags bindingFlags, params string[] memberNames)
{
MultiSetCallInfo callInfo = new MultiSetCallInfo(type, bindingFlags, memberNames);
MultiSetter value = MultiSetters.Get(callInfo);
if (value != null)
return value;
value = (MultiSetter)new MultiSetEmitter(callInfo).GetDelegate();
MultiSetters.Insert(callInfo, value);
return value;
}
/// <summary>
/// Creates a <see cref="Fasterflect.MultiSetter"/> which sets the values of the given members.
/// </summary>
/// <param name="type">The <see cref="Type"/> whose members will be set.</param>
/// <param name="members">The members to set.</param>
/// <returns>A <see cref="Fasterflect.MultiSetter"/> which sets the values of the given members.</returns>
public static MultiSetter MultiSetter(Type type, IList<MemberInfo> members)
{
MultiSetCallInfo callInfo = new MultiSetCallInfo(type, members);
MultiSetter value = MultiSetters.Get(callInfo);
if (value != null)
return value;
value = (MultiSetter)new MultiSetEmitter(type, members).GetDelegate();
MultiSetters.Insert(callInfo, value);
return value;
}
#endregion
#region Indexers
/// <summary>
/// Creates a delegate which can get the value of an indexer.
/// </summary>
/// <param name="type">The type which the indexer belongs to.</param>
/// <param name="parameterTypes">The types of the indexer parameters (must be in the right order).</param>
/// <returns>The delegate which can get the value of an indexer.</returns>
public static MethodInvoker IndexerGetter(Type type, params Type[] parameterTypes)
{
return Method(type, "get_Item", FasterflectFlags.StaticInstanceAnyVisibility, null, null, parameterTypes);
}
/// <summary>
/// Creates a delegate which can get the value of an indexer matching <paramref name="bindingFlags"/>.
/// </summary>
/// <param name="type">The type which the indexer belongs to.</param>
/// <param name="bindingFlags">The <see cref="BindingFlags"/> or <see cref="FasterflectFlags"/> used to lookup the indexer.</param>
/// <param name="parameterTypes">The types of the indexer parameters (must be in the right order).</param>
/// <returns>The delegate which can get the value of an indexer.</returns>
public static MethodInvoker IndexerGetter(Type type, FasterflectFlags bindingFlags, params Type[] parameterTypes)
{
return Method(type, "get_Item", bindingFlags, null, null, parameterTypes);
}
/// <summary>
/// Creates a delegate which can set an indexer
/// </summary>
/// <param name="type">The type which the indexer belongs to.</param>
/// <param name="parameterTypes">The types of the indexer parameters (must be in the right order), plus
/// the type of the indexer.</param>
/// <returns>A delegate which can set an indexer.</returns>
public static MethodInvoker IndexerSetter(Type type, params Type[] parameterTypes)
{
return Method(type, "set_Item", FasterflectFlags.StaticInstanceAnyVisibility, null, null, parameterTypes);
}
/// <summary>
/// Creates a delegate which can set an indexer matching <paramref name="bindingFlags"/>.
/// </summary>
/// <param name="type">The type which the indexer belongs to.</param>
/// <param name="bindingFlags">The <see cref="BindingFlags"/> or <see cref="FasterflectFlags"/> used to lookup the indexer.</param>
/// <param name="parameterTypes">The types of the indexer parameters (must be in the right order), plus
/// the type of the indexer.</param>
/// <returns>A delegate which can set an indexer.</returns>
/// <example>
/// If the indexer is of type <see cref="string"/> and accepts one parameter of type <see langword="int"/>, this
/// method should be invoked as follow:
/// <code>
/// MethodInvoker invoker = type.DelegateForSetIndexer(new Type[]{typeof(int), typeof(string)});
/// </code>
/// </example>
public static MethodInvoker IndexerSetter(Type type, FasterflectFlags bindingFlags, params Type[] parameterTypes)
{
return Method(type, "set_Item", bindingFlags, null, null, parameterTypes);
}
#endregion
#region Methods
internal static MethodInvoker Method(Type type, string name, FasterflectFlags bindingFlags, MethodInfo method, Type[] genericTypes, Type[] parameterTypes)
{
genericTypes = genericTypes ?? Type.EmptyTypes;
CallInfo info = new CallInfo(type, name, bindingFlags, genericTypes, parameterTypes);
MethodInvoker value = Methods.Get(info);
if (value != null)
return value;
method = method ?? ReflectLookup.Method(type, genericTypes, name, parameterTypes, bindingFlags) ?? throw new MissingMethodException(type.FullName, name);
value = (MethodInvoker)new MethodInvocationEmitter(method).GetDelegate();
Methods.Insert(info, value);
return value;
}
/// <summary>
/// Creates a <see cref="MethodInvoker"/> which invokes the given <see cref="MethodInfo"/>.
/// </summary>
/// <param name="method">The <see cref="MethodInfo"/> to invoke.</param>
/// <returns>A <see cref="MethodInvoker"/> which invokes the given <see cref="MethodInfo"/>.</returns>
public static MethodInvoker Method(MethodInfo method)
{
return Method(method.DeclaringType, "set_Item", FasterflectFlags.StaticInstanceAnyVisibility, method, method.GetGenericArguments(), method.GetParameters().ToTypeArray());
}
/// <summary>
/// Creates a <see cref="MethodInvoker"/> which invokes the given <see cref="MethodInfo"/>.
/// </summary>
/// <param name="type">The <see cref="Type"/> that the object that has the <see cref="MethodInfo"/>.</param>
/// <param name="name">The name of the <see cref="MethodInfo"/>.</param>
/// <param name="parameterTypes">The <see cref="Type"/>s of <see cref="MethodInfo"/>'s parameters.</param>
/// <returns>A <see cref="MethodInvoker"/> which invokes the given <see cref="MethodInfo"/>.</returns>
public static MethodInvoker Method(Type type, string name, params Type[] parameterTypes)
{
return Method(type, name, FasterflectFlags.StaticInstanceAnyVisibility, null, null, parameterTypes);
}
/// <summary>
/// Creates a <see cref="MethodInvoker"/> which invokes the given <see cref="MethodInfo"/>.
/// </summary>
/// <param name="type">The <see cref="Type"/> that the object that has the <see cref="MethodInfo"/>.</param>
/// <param name="name">The name of the <see cref="MethodInfo"/>.</param>
/// <param name="genericTypes">The generic <see cref="Type"/>s of the <see cref="MethodInfo"/>'s parameters.</param>
/// <param name="parameterTypes">The <see cref="Type"/>s of the <see cref="MethodInfo"/>'s parameters.</param>
/// <returns>A <see cref="MethodInvoker"/> which invokes the given <see cref="MethodInfo"/>.</returns>
public static MethodInvoker Method(Type type, string name, Type[] genericTypes, params Type[] parameterTypes)
{
return Method(type, name, FasterflectFlags.StaticInstanceAnyVisibility, null, genericTypes, parameterTypes);
}
/// <summary>
/// Creates a <see cref="MethodInvoker"/> which invokes the given <see cref="MethodInfo"/>.
/// </summary>
/// <param name="type">The <see cref="Type"/> that the object that has the <see cref="MethodInfo"/>.</param>
/// <param name="name">The name of the <see cref="MethodInfo"/>.</param>
/// <param name="bindingFlags">The <see cref="BindingFlags"/> or <see cref="FasterflectFlags"/> to filter the <see cref="MethodInfo"/>.</param>
/// <param name="parameterTypes">The <see cref="Type"/>s of the <see cref="MethodInfo"/>'s parameters.</param>
/// <returns>A <see cref="MethodInvoker"/> which invokes the given <see cref="MethodInfo"/>.</returns>
public static MethodInvoker Method(Type type, string name, FasterflectFlags bindingFlags, params Type[] parameterTypes)
{
return Method(type, name, bindingFlags, null, null, parameterTypes);
}
/// <summary>
/// Creates a <see cref="MethodInvoker"/> which invokes the given <see cref="MethodInfo"/>.
/// </summary>
/// <param name="type">The <see cref="Type"/> that the object that has the <see cref="MethodInfo"/>.</param>
/// <param name="name">The name of the <see cref="MethodInfo"/>.</param>
/// <param name="bindingFlags">The <see cref="BindingFlags"/> or <see cref="FasterflectFlags"/> to filter the <see cref="MethodInfo"/>.</param>
/// <param name="genericTypes">The generic <see cref="Type"/>s of the <see cref="MethodInfo"/>'s parameters.</param>
/// <param name="parameterTypes">The <see cref="Type"/>s of the <see cref="MethodInfo"/>'s parameters.</param>
/// <returns>A <see cref="MethodInvoker"/> which invokes the given <see cref="MethodInfo"/>.</returns>
public static MethodInvoker Method(Type type, Type[] genericTypes, string name, FasterflectFlags bindingFlags, params Type[] parameterTypes)
{
return Method(type, name, bindingFlags, null, genericTypes, parameterTypes);
}
#endregion
#region Array Access
/// <summary>
/// Creates an <see cref="ArrayElementGetter"/> which retrieves an element of an array.
/// </summary>
/// <param name="arrayType">The <see cref="Type"/> of the array's elements.</param>
/// <returns>An <see cref="ArrayElementGetter"/> which retrieves an element of an array.</returns>
public static ArrayElementGetter ArrayGetter(Type arrayType)
{
ArrayElementGetter value = ArrayGetters.Get(arrayType);
if (value != null)
return value;
value = (ArrayElementGetter)new ArrayGetEmitter(arrayType).GetDelegate();
ArrayGetters.Insert(arrayType, value);
return value;
}
/// <summary>
/// Creates an <see cref="ArrayElementGetter"/> which sets an element of an array.
/// </summary>
/// <param name="arrayType">The <see cref="Type"/> of the array's elements.</param>
/// <returns>An <see cref="ArrayElementGetter"/> which sets an element of an array.</returns>
public static ArrayElementSetter ArraySetter(Type arrayType)
{
ArrayElementSetter value = ArraySetters.Get(arrayType);
if (value != null)
return value;
value = (ArrayElementSetter)new ArraySetEmitter(arrayType).GetDelegate();
ArraySetters.Insert(arrayType, value);
return value;
}
#endregion
#region ObjectMappers
/// <summary>
/// Creates a delegate that can map values from fields and properties on the source object to fields and properties with the
/// same name on the target object.
/// </summary>
/// <param name="sourceType">The type of the source object.</param>
/// <param name="targetType">The type of the target object.</param>
/// <param name="names">The optional list of member names against which to filter the members that are
/// to be mapped. If this parameter is an empty list then no name filtering will be applied. The default
/// behavior is to check for an exact, case-sensitive match.</param>
/// <returns>An <see cref="ObjectMapper"/> which sets the target using the matching source members.</returns>
public static ObjectMapper Mapper(Type sourceType, Type targetType, params string[] names)
{
ObjectMapper value = Mapper(sourceType, targetType, FasterflectFlags.InstanceAnyVisibility, names, Constants.EmptyStringArray);
return value;
}
/// <summary>
/// Creates a delegate that can map values from fields and properties on the source object to fields and properties with the
/// same name on the target object.
/// </summary>
/// <param name="sourceType">The type of the source object.</param>
/// <param name="targetType">The type of the target object.</param>
/// <param name="sourceNames">The member names (Fields, Properties or both) to include on the source.</param>
/// <param name="targetNames">The member names (Fields, Properties or both) to include on the target.</param>
/// <returns>An <see cref="ObjectMapper"/> which sets the target using the matching source members.</returns>
public static ObjectMapper Mapper(Type sourceType, Type targetType, string[] sourceNames, string[] targetNames)
{
ObjectMapper value = Mapper(sourceType, targetType, FasterflectFlags.InstanceAnyVisibility, sourceNames, targetNames);
return value;
}
/// <summary>
/// Creates a delegate that can map values from fields and properties on the source object to fields and properties with the
/// same name on the target object.
/// </summary>
/// <param name="sourceType">The type of the source object.</param>
/// <param name="targetType">The type of the target object.</param>
/// <param name="bindingFlags">The <see cref="BindingFlags"/> or <see cref="FasterflectFlags"/> to filter the members.</param>
/// <param name="names">The optional list of member names against which to filter the members that are
/// to be mapped. If this parameter is an empty string then no name filtering will be applied.</param>
/// <returns>An <see cref="ObjectMapper"/> which sets the target using the matching source members.</returns>
/// <returns></returns>
public static ObjectMapper Mapper(Type sourceType, Type targetType, FasterflectFlags bindingFlags, params string[] names)
{
ObjectMapper value = Mapper(sourceType, targetType, bindingFlags, names, names);
return value;
}
/// <summary>
/// Creates a delegate that can map values from fields and properties on the source object to fields and properties on the target object.
/// </summary>
/// <param name="sourceType">The type of the source object.</param>
/// <param name="targetType">The type of the target object.</param>
/// <param name="bindingFlags">The <see cref="BindingFlags"/> or <see cref="FasterflectFlags"/> to filter the members.</param>
/// <param name="sourceNames">The member names (Fields, Properties or both) to include on the source.</param>
/// <param name="targetNames">The member names (Fields, Properties or both) to include on the target.</param>
/// <returns>An <see cref="ObjectMapper"/> which sets the target using the matching source members.</returns>
internal static ObjectMapper Mapper(Type sourceType, Type targetType, FasterflectFlags bindingFlags, string[] sourceNames, string[] targetNames)
{
MapCallInfo info = new MapCallInfo(sourceType, targetType, bindingFlags, sourceNames, targetNames);
ObjectMapper value = Mappers.Get(info);
if (value != null)
return value;
value = (ObjectMapper)new MapEmitter(info).GetDelegate();
Mappers.Insert(info, value);
return value;
}
#endregion
#region Clone
/// <summary>
/// Produces a deep clone of the <paramref name="source"/> object. Reference integrity is maintained and
/// every unique object in the graph is cloned only once. All objects in the graph must have a default constructor.
/// </summary>
/// <typeparam name="T">The type of the object to clone.</typeparam>
/// <param name="source">The object to clone.</param>
/// <returns>A deep clone of the source object.</returns>
public static T DeepClone<T>(T source) where T : class, new()
{
if (source == null)
return null;
T clone = Extensions.DeepClone.DeepCloneExtensions.DeepClone(source);
return clone;
}
/// <summary>
/// Clones an object via shallow copy.
/// </summary>
/// <typeparam name="T">The type of object to clone.</typeparam>
/// <param name="obj">The object to clone</param>
/// <returns>A shallow clone of the source object.</returns>
public static T ShallowClone<T>(T obj) where T : class
{
if (obj == null)
return null;
MethodInfo inst = obj.GetType().GetMethod("MemberwiseClone", BindingFlags.Instance | BindingFlags.NonPublic);
T clone = (T)inst?.Invoke(obj, null);
return clone;
}
#endregion
}
}