This repository has been archived by the owner on Sep 14, 2018. It is now read-only.
/
ReflectionUtils.cs
1932 lines (1613 loc) · 76.6 KB
/
ReflectionUtils.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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ****************************************************************************
*
* Copyright (c) Microsoft Corporation.
*
* This source code is subject to terms and conditions of the Apache License, Version 2.0. A
* copy of the license can be found in the License.html file at the root of this distribution. If
* you cannot locate the Apache License, Version 2.0, please send an email to
* dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
* by the terms of the Apache License, Version 2.0.
*
* You must not remove this notice, or any other, from this software.
*
*
* ***************************************************************************/
#if FEATURE_METADATA_READER
using Microsoft.Scripting.Metadata;
#endif
#if !WIN8
using TypeInfo = System.Type;
#endif
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Security;
using System.Text;
using System.Runtime.InteropServices;
using System.Dynamic;
using System.Linq.Expressions;
using Microsoft.Scripting.Generation;
using Microsoft.Scripting.Runtime;
using Microsoft.Scripting.Utils;
#if WIN8 || WP75
namespace System.Runtime.CompilerServices {
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event)]
public sealed class SpecialNameAttribute : Attribute {
public SpecialNameAttribute() {
}
}
}
#endif
#if WIN8
namespace System {
public enum TypeCode {
Empty,
Object,
DBNull,
Boolean,
Char,
SByte,
Byte,
Int16,
UInt16,
Int32,
UInt32,
Int64,
UInt64,
Single,
Double,
Decimal,
DateTime,
String = 18
}
}
namespace System.Reflection {
[Flags]
public enum BindingFlags {
/// <summary>Specifies that instance members are to be included in the search.</summary>
Instance = 4,
/// <summary>Specifies that static members are to be included in the search.</summary>
Static = 8,
/// <summary>Specifies that public members are to be included in the search.</summary>
Public = 16,
/// <summary>Specifies that non-public members are to be included in the search.</summary>
NonPublic = 32
}
}
#else
namespace System.Reflection {
public static class RuntimeReflectionExtensions {
public static MethodInfo GetRuntimeBaseDefinition(this MethodInfo method) {
return method.GetBaseDefinition();
}
public static IEnumerable<MethodInfo> GetRuntimeMethods(this Type type) {
return type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
}
}
}
#endif
namespace Microsoft.Scripting.Utils {
// CF doesn't support DefaultParameterValue attribute. Define our own, but not in System.Runtime.InteropServices namespace as that would
// make C# compiler emit the parameter's default value metadata not the attribute itself. The default value metadata are not accessible on CF.
#if !FEATURE_DEFAULT_PARAMETER_VALUE
/// <summary>
/// The Default Parameter Value Attribute.
/// </summary>
public sealed class DefaultParameterValueAttribute : Attribute
{
private readonly object _value;
public object Value
{
get { return _value; }
}
/// <summary>
/// The constructor
/// </summary>
/// <param name="value">The value.</param>
public DefaultParameterValueAttribute(object value)
{
_value = value;
}
}
#if !ANDROID
[AttributeUsage(AttributeTargets.Parameter, Inherited = false), ComVisible(true)]
public sealed class OptionalAttribute : Attribute {
}
#endif
#endif
public static class ReflectionUtils {
#region Accessibility
public static BindingFlags AllMembers = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
public static bool IsPublic(this PropertyInfo property) {
return property.GetGetMethod(nonPublic: false) != null
|| property.GetSetMethod(nonPublic: false) != null;
}
public static bool IsStatic(this PropertyInfo property) {
var getter = property.GetGetMethod(nonPublic: true);
var setter = property.GetSetMethod(nonPublic: true);
return getter != null && getter.IsStatic
|| setter != null && setter.IsStatic;
}
public static bool IsStatic(this EventInfo evnt) {
var add = evnt.GetAddMethod(nonPublic: true);
var remove = evnt.GetRemoveMethod(nonPublic: true);
return add != null && add.IsStatic
|| remove != null && remove.IsStatic;
}
public static bool IsPrivate(this PropertyInfo property) {
var getter = property.GetGetMethod(nonPublic: true);
var setter = property.GetSetMethod(nonPublic: true);
return (getter == null || getter.IsPrivate)
&& (setter == null || setter.IsPrivate);
}
public static bool IsPrivate(this EventInfo evnt) {
var add = evnt.GetAddMethod(nonPublic: true);
var remove = evnt.GetRemoveMethod(nonPublic: true);
return (add == null || add.IsPrivate)
&& (remove == null || remove.IsPrivate);
}
private static bool MatchesFlags(ConstructorInfo member, BindingFlags flags) {
return
((member.IsPublic ? BindingFlags.Public : BindingFlags.NonPublic) & flags) != 0 &&
((member.IsStatic ? BindingFlags.Static : BindingFlags.Instance) & flags) != 0;
}
private static bool MatchesFlags(MethodInfo member, BindingFlags flags) {
return
((member.IsPublic ? BindingFlags.Public : BindingFlags.NonPublic) & flags) != 0 &&
((member.IsStatic ? BindingFlags.Static : BindingFlags.Instance) & flags) != 0;
}
private static bool MatchesFlags(FieldInfo member, BindingFlags flags) {
return
((member.IsPublic ? BindingFlags.Public : BindingFlags.NonPublic) & flags) != 0 &&
((member.IsStatic ? BindingFlags.Static : BindingFlags.Instance) & flags) != 0;
}
private static bool MatchesFlags(PropertyInfo member, BindingFlags flags) {
return
((member.IsPublic() ? BindingFlags.Public : BindingFlags.NonPublic) & flags) != 0 &&
((member.IsStatic() ? BindingFlags.Static : BindingFlags.Instance) & flags) != 0;
}
private static bool MatchesFlags(EventInfo member, BindingFlags flags) {
var add = member.GetAddMethod();
var remove = member.GetRemoveMethod();
var raise = member.GetRaiseMethod();
bool isPublic = add != null && add.IsPublic || remove != null && remove.IsPublic || raise != null && raise.IsPublic;
bool isStatic = add != null && add.IsStatic || remove != null && remove.IsStatic || raise != null && raise.IsStatic;
return
((isPublic ? BindingFlags.Public : BindingFlags.NonPublic) & flags) != 0 &&
((isStatic ? BindingFlags.Static : BindingFlags.Instance) & flags) != 0;
}
private static bool MatchesFlags(TypeInfo member, BindingFlags flags) {
// Static/Instance are ignored
return (((member.IsPublic || member.IsNestedPublic) ? BindingFlags.Public : BindingFlags.NonPublic) & flags) != 0;
}
private static bool MatchesFlags(MemberInfo member, BindingFlags flags) {
ConstructorInfo ctor;
MethodInfo method;
FieldInfo field;
EventInfo evnt;
PropertyInfo property;
if ((method = member as MethodInfo) != null) {
return MatchesFlags(method, flags);
}
if ((field = member as FieldInfo) != null) {
return MatchesFlags(field, flags);
}
if ((ctor = member as ConstructorInfo) != null) {
return MatchesFlags(ctor, flags);
}
if ((evnt = member as EventInfo) != null) {
return MatchesFlags(evnt, flags);
}
if ((property = member as PropertyInfo) != null) {
return MatchesFlags(property, flags);
}
return MatchesFlags((TypeInfo)member, flags);
}
private static IEnumerable<T> WithBindingFlags<T>(this IEnumerable<T> members, Func<T, BindingFlags, bool> matchFlags, BindingFlags flags)
where T : MemberInfo {
return members.Where(member => matchFlags(member, flags));
}
public static IEnumerable<MemberInfo> WithBindingFlags(this IEnumerable<MemberInfo> members, BindingFlags flags) {
return members.WithBindingFlags(MatchesFlags, flags);
}
public static IEnumerable<MethodInfo> WithBindingFlags(this IEnumerable<MethodInfo> members, BindingFlags flags) {
return members.WithBindingFlags(MatchesFlags, flags);
}
public static IEnumerable<ConstructorInfo> WithBindingFlags(this IEnumerable<ConstructorInfo> members, BindingFlags flags) {
return members.WithBindingFlags(MatchesFlags, flags);
}
public static IEnumerable<FieldInfo> WithBindingFlags(this IEnumerable<FieldInfo> members, BindingFlags flags) {
return members.WithBindingFlags(MatchesFlags, flags);
}
public static IEnumerable<PropertyInfo> WithBindingFlags(this IEnumerable<PropertyInfo> members, BindingFlags flags) {
return members.WithBindingFlags(MatchesFlags, flags);
}
public static IEnumerable<EventInfo> WithBindingFlags(this IEnumerable<EventInfo> members, BindingFlags flags) {
return members.WithBindingFlags(MatchesFlags, flags);
}
public static IEnumerable<TypeInfo> WithBindingFlags(this IEnumerable<TypeInfo> members, BindingFlags flags) {
return members.WithBindingFlags(MatchesFlags, flags);
}
public static MemberInfo WithBindingFlags(this MemberInfo member, BindingFlags flags) {
return member != null && MatchesFlags(member, flags) ? member : null;
}
public static MethodInfo WithBindingFlags(this MethodInfo member, BindingFlags flags) {
return member != null && MatchesFlags(member, flags) ? member : null;
}
public static ConstructorInfo WithBindingFlags(this ConstructorInfo member, BindingFlags flags) {
return member != null && MatchesFlags(member, flags) ? member : null;
}
public static FieldInfo WithBindingFlags(this FieldInfo member, BindingFlags flags) {
return member != null && MatchesFlags(member, flags) ? member : null;
}
public static PropertyInfo WithBindingFlags(this PropertyInfo member, BindingFlags flags) {
return member != null && MatchesFlags(member, flags) ? member : null;
}
public static EventInfo WithBindingFlags(this EventInfo member, BindingFlags flags) {
return member != null && MatchesFlags(member, flags) ? member : null;
}
public static TypeInfo WithBindingFlags(this TypeInfo member, BindingFlags flags) {
return member != null && MatchesFlags(member, flags) ? member : null;
}
#endregion
#region Signatures
public static IEnumerable<MethodInfo> WithSignature(this IEnumerable<MethodInfo> members, Type[] parameterTypes) {
return members.Where(c => {
var ps = c.GetParameters();
if (ps.Length != parameterTypes.Length) {
return false;
}
for (int i = 0; i < ps.Length; i++) {
if (parameterTypes[i] != ps[i].ParameterType) {
return false;
}
}
return true;
});
}
public static IEnumerable<ConstructorInfo> WithSignature(this IEnumerable<ConstructorInfo> members, Type[] parameterTypes) {
return members.Where(c => {
var ps = c.GetParameters();
if (ps.Length != parameterTypes.Length) {
return false;
}
for (int i = 0; i < ps.Length; i++) {
if (parameterTypes[i] != ps[i].ParameterType) {
return false;
}
}
return true;
});
}
#endregion
#region Member Inheritance
// CLI specification, partition I, 8.10.4: Hiding, overriding, and layout
// ----------------------------------------------------------------------
// While hiding applies to all members of a type, overriding deals with object layout and is applicable only to instance fields
// and virtual methods. The CTS provides two forms of member overriding, new slot and expect existing slot. A member of a derived
// type that is marked as a new slot will always get a new slot in the objects layout, guaranteeing that the base field or method
// is available in the object by using a qualified reference that combines the name of the base type with the name of the member
// and its type or signature. A member of a derived type that is marked as expect existing slot will re-use (i.e., share or override)
// a slot that corresponds to a member of the same kind (field or method), name, and type if one already exists from the base type;
// if no such slot exists, a new slot is allocated and used.
//
// The general algorithm that is used for determining the names in a type and the layout of objects of the type is roughly as follows:
// - Flatten the inherited names (using the hide by name or hide by name-and-signature rule) ignoring accessibility rules.
// - For each new member that is marked expect existing slot, look to see if an exact match on kind (i.e., field or method),
// name, and signature exists and use that slot if it is found, otherwise allocate a new slot.
// - After doing this for all new members, add these new member-kind/name/signatures to the list of members of this type
// - Finally, remove any inherited names that match the new members based on the hide by name or hide by name-and-signature rules.
// NOTE: Following GetXxx only implement overriding, not hiding specified by hide-by-name or hide-by-name-and-signature flags.
public static IEnumerable<MethodInfo> GetInheritedMethods(this Type type, string name = null, bool flattenHierarchy = false) {
while (type.IsGenericParameter) {
type = type.GetBaseType();
}
var baseDefinitions = new HashSet<MethodInfo>(ReferenceEqualityComparer<MethodInfo>.Instance);
foreach (var ancestor in type.Ancestors()) {
foreach (var declaredMethod in ancestor.GetDeclaredMethods(name)) {
if (declaredMethod != null && IncludeMethod(declaredMethod, type, baseDefinitions, flattenHierarchy)) {
yield return declaredMethod;
}
}
}
}
private static bool IncludeMethod(MethodInfo member, Type reflectedType, HashSet<MethodInfo> baseDefinitions, bool flattenHierarchy) {
if (member.IsVirtual) {
if (baseDefinitions.Add(RuntimeReflectionExtensions.GetRuntimeBaseDefinition(member))) {
return true;
}
} else if (member.DeclaringType == reflectedType) {
return true;
} else if (!member.IsPrivate && (!member.IsStatic || flattenHierarchy)) {
return true;
}
return false;
}
public static IEnumerable<PropertyInfo> GetInheritedProperties(this Type type, string name = null, bool flattenHierarchy = false) {
while (type.IsGenericParameter) {
type = type.GetBaseType();
}
var baseDefinitions = new HashSet<MethodInfo>(ReferenceEqualityComparer<MethodInfo>.Instance);
foreach (var ancestor in type.Ancestors()) {
if (name != null) {
var declaredProperty = ancestor.GetDeclaredProperty(name);
if (declaredProperty != null && IncludeProperty(declaredProperty, type, baseDefinitions, flattenHierarchy)) {
yield return declaredProperty;
}
} else {
foreach (var declaredProperty in ancestor.GetDeclaredProperties()) {
if (IncludeProperty(declaredProperty, type, baseDefinitions, flattenHierarchy)) {
yield return declaredProperty;
}
}
}
}
}
// CLI spec 22.34 Properties
// -------------------------
// [Note: The CLS (see Partition I) refers to instance, virtual, and static properties.
// The signature of a property (from the Type column) can be used to distinguish a static property,
// since instance and virtual properties will have the HASTHIS bit set in the signature (§23.2.1)
// while a static property will not. The distinction between an instance and a virtual property
// depends on the signature of the getter and setter methods, which the CLS requires to be either
// both virtual or both instance. end note]
private static bool IncludeProperty(PropertyInfo member, Type reflectedType, HashSet<MethodInfo> baseDefinitions, bool flattenHierarchy) {
var getter = member.GetGetMethod(nonPublic: true);
var setter = member.GetSetMethod(nonPublic: true);
MethodInfo virtualAccessor;
if (getter != null && getter.IsVirtual) {
virtualAccessor = getter;
} else if (setter != null && setter.IsVirtual) {
virtualAccessor = setter;
} else {
virtualAccessor = null;
}
if (virtualAccessor != null) {
if (baseDefinitions.Add(RuntimeReflectionExtensions.GetRuntimeBaseDefinition(virtualAccessor))) {
return true;
}
} else if (member.DeclaringType == reflectedType) {
return true;
} else if (!member.IsPrivate() && (!member.IsStatic() || flattenHierarchy)) {
return true;
}
return false;
}
public static IEnumerable<EventInfo> GetInheritedEvents(this Type type, string name = null, bool flattenHierarchy = false) {
while (type.IsGenericParameter) {
type = type.GetBaseType();
}
var baseDefinitions = new HashSet<MethodInfo>(ReferenceEqualityComparer<MethodInfo>.Instance);
foreach (var ancestor in type.Ancestors()) {
if (name != null) {
var declaredEvent = ancestor.GetDeclaredEvent(name);
if (declaredEvent != null && IncludeEvent(declaredEvent, type, baseDefinitions, flattenHierarchy)) {
yield return declaredEvent;
}
} else {
foreach (var declaredEvent in ancestor.GetDeclaredEvents()) {
if (IncludeEvent(declaredEvent, type, baseDefinitions, flattenHierarchy)) {
yield return declaredEvent;
}
}
}
}
}
private static bool IncludeEvent(EventInfo member, Type reflectedType, HashSet<MethodInfo> baseDefinitions, bool flattenHierarchy) {
var add = member.GetAddMethod(nonPublic: true);
var remove = member.GetRemoveMethod(nonPublic: true);
// TOOD: fire method?
MethodInfo virtualAccessor;
if (add != null && add.IsVirtual) {
virtualAccessor = add;
} else if (remove != null && remove.IsVirtual) {
virtualAccessor = remove;
} else {
virtualAccessor = null;
}
if (virtualAccessor != null) {
if (baseDefinitions.Add(RuntimeReflectionExtensions.GetRuntimeBaseDefinition(virtualAccessor))) {
return true;
}
} else if (member.DeclaringType == reflectedType) {
return true;
} else if (!member.IsPrivate() && (!member.IsStatic() || flattenHierarchy)) {
return true;
}
return false;
}
public static IEnumerable<FieldInfo> GetInheritedFields(this Type type, string name = null, bool flattenHierarchy = false) {
while (type.IsGenericParameter) {
type = type.GetBaseType();
}
foreach (var ancestor in type.Ancestors()) {
if (name != null) {
var declaredField = ancestor.GetDeclaredField(name);
if (declaredField != null && IncludeField(declaredField, type, flattenHierarchy)) {
yield return declaredField;
}
} else {
foreach (var declaredField in ancestor.GetDeclaredFields()) {
if (IncludeField(declaredField, type, flattenHierarchy)) {
yield return declaredField;
}
}
}
}
}
private static bool IncludeField(FieldInfo member, Type reflectedType, bool flattenHierarchy) {
if (member.DeclaringType == reflectedType) {
return true;
} else if (!member.IsPrivate && (!member.IsStatic || flattenHierarchy)) {
return true;
}
return false;
}
public static IEnumerable<MemberInfo> GetInheritedMembers(this Type type, string name = null, bool flattenHierarchy = false) {
var result =
type.GetInheritedMethods(name, flattenHierarchy).Cast<MethodInfo, MemberInfo>().Concat(
type.GetInheritedProperties(name, flattenHierarchy).Cast<PropertyInfo, MemberInfo>().Concat(
type.GetInheritedEvents(name, flattenHierarchy).Cast<EventInfo, MemberInfo>().Concat(
type.GetInheritedFields(name, flattenHierarchy).Cast<FieldInfo, MemberInfo>())));
if (name == null) {
return result.Concat<MemberInfo>(
type.GetDeclaredConstructors().Cast<ConstructorInfo, MemberInfo>().Concat(
type.GetDeclaredNestedTypes().Cast<TypeInfo, MemberInfo>()));
}
var nestedType = type.GetDeclaredNestedType(name);
return (nestedType != null) ? result.Concat(new[] { nestedType }) : result;
}
#endregion
#region Declared Members
public static IEnumerable<ConstructorInfo> GetDeclaredConstructors(this Type type) {
#if WIN8
return type.GetTypeInfo().DeclaredConstructors;
#else
return type.GetConstructors(BindingFlags.DeclaredOnly | AllMembers);
#endif
}
#if WIN8
public static ConstructorInfo GetConstructor(this Type type, Type[] parameterTypes) {
return type.GetDeclaredConstructors().Where(ci => !ci.IsStatic && ci.IsPublic).WithSignature(parameterTypes).SingleOrDefault();
}
#endif
public static IEnumerable<MethodInfo> GetDeclaredMethods(this Type type, string name = null) {
#if WIN8
if (name == null) {
return type.GetTypeInfo().DeclaredMethods;
} else {
return type.GetTypeInfo().GetDeclaredMethods(name);
}
#else
if (name == null) {
return type.GetMethods(BindingFlags.DeclaredOnly | AllMembers);
} else {
return type.GetMember(name, MemberTypes.Method, BindingFlags.DeclaredOnly | AllMembers).OfType<MethodInfo>();
}
#endif
}
public static IEnumerable<PropertyInfo> GetDeclaredProperties(this Type type) {
#if WIN8
return type.GetTypeInfo().DeclaredProperties;
#else
return type.GetProperties(BindingFlags.DeclaredOnly | AllMembers);
#endif
}
public static PropertyInfo GetDeclaredProperty(this Type type, string name) {
Debug.Assert(name != null);
#if WIN8
return type.GetTypeInfo().GetDeclaredProperty(name);
#else
return type.GetProperty(name, BindingFlags.DeclaredOnly | AllMembers);
#endif
}
public static IEnumerable<EventInfo> GetDeclaredEvents(this Type type) {
#if WIN8
return type.GetTypeInfo().DeclaredEvents;
#else
return type.GetEvents(BindingFlags.DeclaredOnly | AllMembers);
#endif
}
public static EventInfo GetDeclaredEvent(this Type type, string name) {
Debug.Assert(name != null);
#if WIN8
return type.GetTypeInfo().GetDeclaredEvent(name);
#else
return type.GetEvent(name, BindingFlags.DeclaredOnly | AllMembers);
#endif
}
public static IEnumerable<FieldInfo> GetDeclaredFields(this Type type) {
#if WIN8
return type.GetTypeInfo().DeclaredFields;
#else
return type.GetFields(BindingFlags.DeclaredOnly | AllMembers);
#endif
}
public static FieldInfo GetDeclaredField(this Type type, string name) {
Debug.Assert(name != null);
#if WIN8
return type.GetTypeInfo().GetDeclaredField(name);
#else
return type.GetField(name, BindingFlags.DeclaredOnly | AllMembers);
#endif
}
public static IEnumerable<TypeInfo> GetDeclaredNestedTypes(this Type type) {
#if WIN8
return type.GetTypeInfo().DeclaredNestedTypes;
#else
return type.GetNestedTypes(BindingFlags.DeclaredOnly | AllMembers);
#endif
}
public static TypeInfo GetDeclaredNestedType(this Type type, string name) {
Debug.Assert(name != null);
#if WIN8
return type.GetTypeInfo().GetDeclaredNestedType(name);
#else
return type.GetNestedType(name, BindingFlags.DeclaredOnly | AllMembers);
#endif
}
public static IEnumerable<MemberInfo> GetDeclaredMembers(this Type type, string name = null) {
#if WIN8
var info = type.GetTypeInfo();
if (name == null) {
return info.DeclaredMembers;
} else {
return GetDeclaredMembersWithName(info, name);
}
#else
if (name == null) {
return type.GetMembers(BindingFlags.DeclaredOnly | AllMembers);
} else {
return type.GetMember(name, BindingFlags.DeclaredOnly | AllMembers);
}
#endif
}
#if WIN8
private static IEnumerable<MemberInfo> GetDeclaredMembersWithName(TypeInfo info, string name) {
MemberInfo member;
if ((member = info.GetDeclaredMethod(name)) != null) {
yield return member;
}
if ((member = info.GetDeclaredField(name)) != null) {
yield return member;
}
if ((member = info.GetDeclaredProperty(name)) != null) {
yield return member;
}
if ((member = info.GetDeclaredEvent(name)) != null) {
yield return member;
}
if ((member = info.GetDeclaredNestedType(name)) != null) {
yield return member;
}
}
#endif
#endregion
#region Win8
#if WIN8
public static TypeCode GetTypeCode(this Enum e) {
return GetTypeCode(Enum.GetUnderlyingType(e.GetType()));
}
// TODO: reduce to numeric types?
public static TypeCode GetTypeCode(this Type type) {
if (type == typeof(int)) {
return TypeCode.Int32;
}
if (type == typeof(sbyte)) {
return TypeCode.SByte;
}
if (type == typeof(short)) {
return TypeCode.Int16;
}
if (type == typeof(long)) {
return TypeCode.Int64;
}
if (type == typeof(uint)) {
return TypeCode.UInt32;
}
if (type == typeof(byte)) {
return TypeCode.Byte;
}
if (type == typeof(ushort)) {
return TypeCode.UInt16;
}
if (type == typeof(ulong)) {
return TypeCode.UInt64;
}
if (type == typeof(bool)) {
return TypeCode.Boolean;
}
if (type == typeof(char)) {
return TypeCode.Char;
}
// TODO: do we need this?
if (type == typeof(string)) {
return TypeCode.String;
}
if (type == typeof(bool)) {
return TypeCode.Boolean;
}
if (type == typeof(double)) {
return TypeCode.Double;
}
if (type == typeof(float)) {
return TypeCode.Single;
}
if (type == typeof(decimal)) {
return TypeCode.Decimal;
}
if (type == typeof(DateTime)) {
return TypeCode.DateTime;
}
return TypeCode.Object;
}
public static IEnumerable<Type> GetImplementedInterfaces(this Type type) {
return type.GetTypeInfo().ImplementedInterfaces;
}
public static MethodInfo GetGetMethod(this PropertyInfo propertyInfo, bool nonPublic = false) {
var accessor = propertyInfo.GetMethod;
return nonPublic || accessor == null || accessor.IsPublic ? accessor : null;
}
public static MethodInfo GetSetMethod(this PropertyInfo propertyInfo, bool nonPublic = false) {
var accessor = propertyInfo.SetMethod;
return nonPublic || accessor == null || accessor.IsPublic ? accessor : null;
}
public static MethodInfo GetAddMethod(this EventInfo eventInfo, bool nonPublic = false) {
var accessor = eventInfo.AddMethod;
return nonPublic || accessor == null || accessor.IsPublic ? accessor : null;
}
public static MethodInfo GetRemoveMethod(this EventInfo eventInfo, bool nonPublic = false) {
var accessor = eventInfo.RemoveMethod;
return nonPublic || accessor == null || accessor.IsPublic ? accessor : null;
}
public static MethodInfo GetRaiseMethod(this EventInfo eventInfo, bool nonPublic = false) {
var accessor = eventInfo.RaiseMethod;
return nonPublic || accessor == null || accessor.IsPublic ? accessor : null;
}
public static MethodInfo GetMethod(this Type type, string name) {
return type.GetTypeInfo().GetDeclaredMethod(name);
}
// TODO: FlattenHierarchy
// TODO: inherited!
public static MethodInfo GetMethod(this Type type, string name, Type[] parameterTypes) {
return type.GetTypeInfo().GetDeclaredMethods(name).WithSignature(parameterTypes).Single();
}
public static MethodInfo GetMethod(this Type type, string name, BindingFlags bindingFlags) {
return type.GetMethods(name, bindingFlags).Single();
}
private static IEnumerable<MethodInfo> GetMethods(this Type type, string name, BindingFlags bindingFlags) {
return type.GetTypeInfo().GetDeclaredMethods(name).WithBindingFlags(bindingFlags);
}
public static MethodInfo GetMethod(this Delegate d) {
return d.GetMethodInfo();
}
public static Type[] GetGenericArguments(this Type type) {
return type.GetTypeInfo().GenericTypeArguments;
}
public static bool IsAssignableFrom(this Type type, Type other) {
return type.GetTypeInfo().IsAssignableFrom(other.GetTypeInfo());
}
public static Type[] GetGenericParameterConstraints(this Type type) {
return type.GetTypeInfo().GetGenericParameterConstraints();
}
public static bool IsSubclassOf(this Type type, Type other) {
return type.GetTypeInfo().IsSubclassOf(other);
}
public static IEnumerable<Type> GetInterfaces(this Type type) {
return type.GetTypeInfo().ImplementedInterfaces;
}
public static Type[] GetRequiredCustomModifiers(this ParameterInfo parameter) {
return EmptyTypes;
}
public static Type[] GetOptionalCustomModifiers(this ParameterInfo parameter) {
return EmptyTypes;
}
public static IEnumerable<Module> GetModules(this Assembly assembly) {
return assembly.Modules;
}
private static string GetDefaultMemberName(this Type type) {
foreach (var ancestor in type.Ancestors()) {
var attr = ancestor.GetTypeInfo().GetCustomAttributes<DefaultMemberAttribute>().SingleOrDefault();
if (attr != null) {
return attr.MemberName;
}
}
return null;
}
public static IEnumerable<MemberInfo> GetDefaultMembers(this Type type) {
string defaultMemberName = type.GetDefaultMemberName();
if (defaultMemberName != null) {
return type.GetInheritedMembers(defaultMemberName).WithBindingFlags(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
}
return Enumerable.Empty<MemberInfo>();
}
#else
public static IEnumerable<Module> GetModules(this Assembly assembly) {
return assembly.GetModules();
}
public static IEnumerable<Type> GetImplementedInterfaces(this Type type) {
return type.GetInterfaces();
}
public static TypeCode GetTypeCode(this Type type) {
return Type.GetTypeCode(type);
}
public static MethodInfo GetMethodInfo(this Delegate d) {
return d.Method;
}
public static bool IsDefined(this Assembly assembly, Type attributeType) {
return assembly.IsDefined(attributeType, false);
}
public static T GetCustomAttribute<T>(this Assembly assembly, bool inherit = false) where T : Attribute {
return (T)Attribute.GetCustomAttribute(assembly, typeof(T), inherit);
}
public static T GetCustomAttribute<T>(this MemberInfo member, bool inherit = false) where T : Attribute {
return (T)Attribute.GetCustomAttribute(member, typeof(T), inherit);
}
public static IEnumerable<T> GetCustomAttributes<T>(this Assembly assembly, bool inherit = false) where T : Attribute {
return Attribute.GetCustomAttributes(assembly, typeof(T), inherit).Cast<T>();
}
public static IEnumerable<T> GetCustomAttributes<T>(this MemberInfo member, bool inherit = false) where T : Attribute {
return Attribute.GetCustomAttributes(member, typeof(T), inherit).Cast<T>();
}
#endif
public static bool ContainsGenericParameters(this Type type) {
return type.GetTypeInfo().ContainsGenericParameters;
}
public static bool IsInterface(this Type type) {
return type.GetTypeInfo().IsInterface;
}
public static bool IsClass(this Type type) {
return type.GetTypeInfo().IsClass;
}
public static bool IsGenericType(this Type type) {
return type.GetTypeInfo().IsGenericType;
}
public static bool IsGenericTypeDefinition(this Type type) {
return type.GetTypeInfo().IsGenericTypeDefinition;
}
public static bool IsSealed(this Type type) {
return type.GetTypeInfo().IsSealed;
}
public static bool IsAbstract(this Type type) {
return type.GetTypeInfo().IsAbstract;
}
public static bool IsPublic(this Type type) {
return type.GetTypeInfo().IsPublic;
}
public static bool IsVisible(this Type type) {
return type.GetTypeInfo().IsVisible;
}
public static Type GetBaseType(this Type type) {
return type.GetTypeInfo().BaseType;
}
public static bool IsValueType(this Type type) {
return type.GetTypeInfo().IsValueType;
}
public static bool IsEnum(this Type type) {
return type.GetTypeInfo().IsEnum;
}
public static bool IsPrimitive(this Type type) {
return type.GetTypeInfo().IsPrimitive;
}
public static GenericParameterAttributes GetGenericParameterAttributes(this Type type) {
return type.GetTypeInfo().GenericParameterAttributes;
}
public static Type[] EmptyTypes = new Type[0];
public static object GetRawConstantValue(this FieldInfo field) {
if (!field.IsLiteral) {
throw new ArgumentException(field + " not a literal.");
}
object value = field.GetValue(null);
return field.FieldType.IsEnum() ? UnwrapEnumValue(value) : value;
}
/// <summary>
/// Converts a boxed enum value to the underlying integer value.
/// </summary>
public static object UnwrapEnumValue(object value) {
if (value == null) {
throw new ArgumentNullException("value");
}
switch (value.GetType().GetTypeCode()) {
case TypeCode.Byte:
return System.Convert.ToByte(value);
case TypeCode.Int16:
return System.Convert.ToInt16(value);
case TypeCode.Int32:
return System.Convert.ToInt32(value);
case TypeCode.Int64:
return System.Convert.ToInt64(value);
case TypeCode.SByte:
return System.Convert.ToSByte(value);
case TypeCode.UInt16:
return System.Convert.ToUInt16(value);
case TypeCode.UInt32:
return System.Convert.ToUInt32(value);