forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlatformExtensions.cs
More file actions
1368 lines (1200 loc) · 46.4 KB
/
PlatformExtensions.cs
File metadata and controls
1368 lines (1200 loc) · 46.4 KB
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
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Threading;
using ServiceStack.Text;
namespace ServiceStack
{
public static class PlatformExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsInterface(this Type type)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return type.GetTypeInfo().IsInterface;
#else
return type.IsInterface;
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsArray(this Type type)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return type.GetTypeInfo().IsArray;
#else
return type.IsArray;
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsValueType(this Type type)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return type.GetTypeInfo().IsValueType;
#else
return type.IsValueType;
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsGeneric(this Type type)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return type.GetTypeInfo().IsGenericType;
#else
return type.IsGenericType;
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Type BaseType(this Type type)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return type.GetTypeInfo().BaseType;
#else
return type.BaseType;
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Type ReflectedType(this PropertyInfo pi)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return pi.DeclaringType;
#else
return pi.ReflectedType;
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Type ReflectedType(this FieldInfo fi)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return fi.DeclaringType;
#else
return fi.ReflectedType;
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Type GenericTypeDefinition(this Type type)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return type.GetTypeInfo().GetGenericTypeDefinition();
#else
return type.GetGenericTypeDefinition();
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Type[] GetTypeInterfaces(this Type type)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return type.GetTypeInfo().ImplementedInterfaces.ToArray();
#else
return type.GetInterfaces();
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Type[] GetTypeGenericArguments(this Type type)
{
#if (NETFX_CORE || PCL)
return type.GenericTypeArguments;
#else
return type.GetGenericArguments();
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ConstructorInfo GetEmptyConstructor(this Type type)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return type.GetTypeInfo().DeclaredConstructors.FirstOrDefault(c => c.GetParameters().Count() == 0);
#else
return type.GetConstructor(Type.EmptyTypes);
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IEnumerable<ConstructorInfo> GetAllConstructors(this Type type)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return type.GetTypeInfo().DeclaredConstructors;
#else
return type.GetConstructors();
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static PropertyInfo[] GetTypesPublicProperties(this Type subType)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
var pis = new List<PropertyInfo>();
foreach (var pi in subType.GetRuntimeProperties())
{
var mi = pi.GetMethod ?? pi.SetMethod;
if (mi != null && mi.IsStatic) continue;
pis.Add(pi);
}
return pis.ToArray();
#else
return subType.GetProperties(
BindingFlags.FlattenHierarchy |
BindingFlags.Public |
BindingFlags.Instance);
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static PropertyInfo[] GetTypesProperties(this Type subType)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
var pis = new List<PropertyInfo>();
foreach (var pi in subType.GetRuntimeProperties())
{
var mi = pi.GetMethod ?? pi.SetMethod;
if (mi != null && mi.IsStatic) continue;
pis.Add(pi);
}
return pis.ToArray();
#else
return subType.GetProperties(
BindingFlags.FlattenHierarchy |
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance);
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Assembly GetAssembly(this Type type)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return type.GetTypeInfo().Assembly;
#else
return type.Assembly;
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static MethodInfo GetMethod(this Type type, string methodName)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return type.GetTypeInfo().GetDeclaredMethod(methodName);
#else
return type.GetMethod(methodName);
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static FieldInfo[] Fields(this Type type)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return type.GetRuntimeFields().ToArray();
#else
return type.GetFields(
BindingFlags.FlattenHierarchy |
BindingFlags.Instance |
BindingFlags.Static |
BindingFlags.Public |
BindingFlags.NonPublic);
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static PropertyInfo[] Properties(this Type type)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return type.GetRuntimeProperties().ToArray();
#else
return type.GetProperties(
BindingFlags.FlattenHierarchy |
BindingFlags.Instance |
BindingFlags.Static |
BindingFlags.Public |
BindingFlags.NonPublic);
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static FieldInfo[] GetAllFields(this Type type)
{
if (type.IsInterface())
{
return TypeConstants.EmptyFieldInfoArray;
}
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return type.GetRuntimeFields().ToArray();
#else
return type.Fields();
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static FieldInfo[] GetPublicFields(this Type type)
{
if (type.IsInterface())
{
return TypeConstants.EmptyFieldInfoArray;
}
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return type.GetRuntimeFields().Where(p => p.IsPublic && !p.IsStatic).ToArray();
#else
return type.GetFields(BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Instance)
.ToArray();
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static MemberInfo[] GetPublicMembers(this Type type)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
var members = new List<MemberInfo>();
members.AddRange(type.GetRuntimeFields().Where(p => p.IsPublic && !p.IsStatic));
members.AddRange(type.GetPublicProperties());
return members.ToArray();
#else
return type.GetMembers(BindingFlags.Public | BindingFlags.Instance);
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static MemberInfo[] GetAllPublicMembers(this Type type)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
var members = new List<MemberInfo>();
members.AddRange(type.GetRuntimeFields().Where(p => p.IsPublic && !p.IsStatic));
members.AddRange(type.GetPublicProperties());
return members.ToArray();
#else
return type.GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static MethodInfo GetStaticMethod(this Type type, string methodName)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return type.GetMethodInfo(methodName);
#else
return type.GetMethod(methodName, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static MethodInfo GetInstanceMethod(this Type type, string methodName)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return type.GetMethodInfo(methodName);
#else
return type.GetMethod(methodName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static MethodInfo Method(this Delegate fn)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return fn.GetMethodInfo();
#else
return fn.Method;
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool HasAttribute<T>(this Type type)
{
return type.AllAttributes().Any(x => x.GetType() == typeof(T));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool HasAttribute<T>(this PropertyInfo pi)
{
return pi.AllAttributes().Any(x => x.GetType() == typeof(T));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool HasAttribute<T>(this FieldInfo fi)
{
return fi.AllAttributes().Any(x => x.GetType() == typeof(T));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool HasAttributeNamed(this Type type, string name)
{
var normalizedAttr = name.Replace("Attribute", "").ToLower();
return type.AllAttributes().Any(x => x.GetType().Name.Replace("Attribute", "").ToLower() == normalizedAttr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool HasAttributeNamed(this PropertyInfo pi, string name)
{
var normalizedAttr = name.Replace("Attribute", "").ToLower();
return pi.AllAttributes().Any(x => x.GetType().Name.Replace("Attribute", "").ToLower() == normalizedAttr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool HasAttributeNamed(this FieldInfo fi, string name)
{
var normalizedAttr = name.Replace("Attribute", "").ToLower();
return fi.AllAttributes().Any(x => x.GetType().Name.Replace("Attribute", "").ToLower() == normalizedAttr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool HasAttributeNamed(this MemberInfo mi, string name)
{
var normalizedAttr = name.Replace("Attribute", "").ToLower();
return mi.AllAttributes().Any(x => x.GetType().Name.Replace("Attribute", "").ToLower() == normalizedAttr);
}
const string DataContract = "DataContractAttribute";
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsDto(this Type type)
{
if (type == null)
return false;
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return type.HasAttribute<DataContractAttribute>();
#else
return !Env.IsMono
? type.HasAttribute<DataContractAttribute>()
: type.GetCustomAttributes(true).Any(x => x.GetType().Name == DataContract);
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static MethodInfo PropertyGetMethod(this PropertyInfo pi, bool nonPublic = false)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
var mi = pi.GetMethod;
return mi != null && (nonPublic || mi.IsPublic) ? mi : null;
#else
return pi.GetGetMethod(nonPublic);
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Type[] Interfaces(this Type type)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return type.GetTypeInfo().ImplementedInterfaces.ToArray();
//return type.GetTypeInfo().ImplementedInterfaces
// .FirstOrDefault(x => !x.GetTypeInfo().ImplementedInterfaces
// .Any(y => y.GetTypeInfo().ImplementedInterfaces.Contains(y)));
#else
return type.GetInterfaces();
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static PropertyInfo[] AllProperties(this Type type)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return type.GetRuntimeProperties().ToArray();
#else
return type.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
#endif
}
//Should only register Runtime Attributes on StartUp, So using non-ThreadSafe Dictionary is OK
static Dictionary<string, List<Attribute>> propertyAttributesMap
= new Dictionary<string, List<Attribute>>();
static Dictionary<Type, List<Attribute>> typeAttributesMap
= new Dictionary<Type, List<Attribute>>();
public static void ClearRuntimeAttributes()
{
propertyAttributesMap = new Dictionary<string, List<Attribute>>();
typeAttributesMap = new Dictionary<Type, List<Attribute>>();
}
internal static string UniqueKey(this PropertyInfo pi)
{
if (pi.DeclaringType == null)
throw new ArgumentException("Property '{0}' has no DeclaringType".Fmt(pi.Name));
return pi.DeclaringType.Namespace + "." + pi.DeclaringType.Name + "." + pi.Name;
}
public static Type AddAttributes(this Type type, params Attribute[] attrs)
{
List<Attribute> typeAttrs;
if (!typeAttributesMap.TryGetValue(type, out typeAttrs))
{
typeAttributesMap[type] = typeAttrs = new List<Attribute>();
}
typeAttrs.AddRange(attrs);
return type;
}
/// <summary>
/// Add a Property attribute at runtime.
/// <para>Not threadsafe, should only add attributes on Startup.</para>
/// </summary>
public static PropertyInfo AddAttributes(this PropertyInfo propertyInfo, params Attribute[] attrs)
{
List<Attribute> propertyAttrs;
var key = propertyInfo.UniqueKey();
if (!propertyAttributesMap.TryGetValue(key, out propertyAttrs))
{
propertyAttributesMap[key] = propertyAttrs = new List<Attribute>();
}
propertyAttrs.AddRange(attrs);
return propertyInfo;
}
/// <summary>
/// Add a Property attribute at runtime.
/// <para>Not threadsafe, should only add attributes on Startup.</para>
/// </summary>
public static PropertyInfo ReplaceAttribute(this PropertyInfo propertyInfo, Attribute attr)
{
var key = propertyInfo.UniqueKey();
List<Attribute> propertyAttrs;
if (!propertyAttributesMap.TryGetValue(key, out propertyAttrs))
{
propertyAttributesMap[key] = propertyAttrs = new List<Attribute>();
}
propertyAttrs.RemoveAll(x => x.GetType() == attr.GetType());
propertyAttrs.Add(attr);
return propertyInfo;
}
public static List<TAttr> GetAttributes<TAttr>(this PropertyInfo propertyInfo)
{
List<Attribute> propertyAttrs;
return !propertyAttributesMap.TryGetValue(propertyInfo.UniqueKey(), out propertyAttrs)
? new List<TAttr>()
: propertyAttrs.OfType<TAttr>().ToList();
}
public static List<Attribute> GetAttributes(this PropertyInfo propertyInfo)
{
List<Attribute> propertyAttrs;
return !propertyAttributesMap.TryGetValue(propertyInfo.UniqueKey(), out propertyAttrs)
? new List<Attribute>()
: propertyAttrs.ToList();
}
public static List<Attribute> GetAttributes(this PropertyInfo propertyInfo, Type attrType)
{
List<Attribute> propertyAttrs;
return !propertyAttributesMap.TryGetValue(propertyInfo.UniqueKey(), out propertyAttrs)
? new List<Attribute>()
: propertyAttrs.Where(x => attrType.IsInstanceOf(x.GetType())).ToList();
}
public static object[] AllAttributes(this PropertyInfo propertyInfo)
{
#if (NETFX_CORE || PCL)
return propertyInfo.GetCustomAttributes(true).ToArray();
#else
#if NETSTANDARD1_1
var attrs = propertyInfo.GetCustomAttributes(true).ToArray();
#else
var attrs = propertyInfo.GetCustomAttributes(true);
#endif
var runtimeAttrs = propertyInfo.GetAttributes();
if (runtimeAttrs.Count == 0)
return attrs;
runtimeAttrs.AddRange(attrs.Cast<Attribute>());
return runtimeAttrs.Cast<object>().ToArray();
#endif
}
public static object[] AllAttributes(this PropertyInfo propertyInfo, Type attrType)
{
#if (NETFX_CORE || PCL)
return propertyInfo.GetCustomAttributes(true).Where(x => x.GetType().IsInstanceOf(attrType)).ToArray();
#else
#if NETSTANDARD1_1
var attrs = propertyInfo.GetCustomAttributes(attrType, true).ToArray();
#else
var attrs = propertyInfo.GetCustomAttributes(attrType, true);
#endif
var runtimeAttrs = propertyInfo.GetAttributes(attrType);
if (runtimeAttrs.Count == 0)
return attrs;
runtimeAttrs.AddRange(attrs.Cast<Attribute>());
return runtimeAttrs.Cast<object>().ToArray();
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static object[] AllAttributes(this ParameterInfo paramInfo)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return paramInfo.GetCustomAttributes(true).ToArray();
#else
return paramInfo.GetCustomAttributes(true);
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static object[] AllAttributes(this FieldInfo fieldInfo)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return fieldInfo.GetCustomAttributes(true).ToArray();
#else
return fieldInfo.GetCustomAttributes(true);
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static object[] AllAttributes(this MemberInfo memberInfo)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return memberInfo.GetCustomAttributes(true).ToArray();
#else
return memberInfo.GetCustomAttributes(true);
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static object[] AllAttributes(this ParameterInfo paramInfo, Type attrType)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return paramInfo.GetCustomAttributes(true).Where(x => x.GetType().IsInstanceOf(attrType)).ToArray();
#else
return paramInfo.GetCustomAttributes(attrType, true);
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static object[] AllAttributes(this MemberInfo memberInfo, Type attrType)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return memberInfo.GetCustomAttributes(true).Where(x => x.GetType().IsInstanceOf(attrType)).ToArray();
#else
var prop = memberInfo as PropertyInfo;
if (prop != null)
return prop.AllAttributes(attrType);
return memberInfo.GetCustomAttributes(attrType, true);
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static object[] AllAttributes(this FieldInfo fieldInfo, Type attrType)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return fieldInfo.GetCustomAttributes(true).Where(x => x.GetType().IsInstanceOf(attrType)).ToArray();
#else
return fieldInfo.GetCustomAttributes(attrType, true);
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static object[] AllAttributes(this Type type)
{
#if (NETFX_CORE || PCL)
return type.GetTypeInfo().GetCustomAttributes(true).ToArray();
#elif NETSTANDARD1_1
return type.GetTypeInfo().GetCustomAttributes(true).Union(type.GetRuntimeAttributes()).ToArray();
#else
return type.GetCustomAttributes(true).Union(type.GetRuntimeAttributes()).ToArray();
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static object[] AllAttributes(this Type type, Type attrType)
{
#if (NETFX_CORE || PCL)
return type.GetTypeInfo().GetCustomAttributes(true).Where(x => x.GetType().IsInstanceOf(attrType)).ToArray();
#elif NETSTANDARD1_1
return type.GetTypeInfo().GetCustomAttributes(attrType, true)
.Union(type.GetRuntimeAttributes(attrType))
.ToArray();
#else
return type.GetCustomAttributes(attrType, true).Union(type.GetRuntimeAttributes(attrType)).ToArray();
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static object[] AllAttributes(this Assembly assembly)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return assembly.GetCustomAttributes().ToArray();
#else
return assembly.GetCustomAttributes(true).ToArray();
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TAttr[] AllAttributes<TAttr>(this ParameterInfo pi)
{
return pi.AllAttributes(typeof(TAttr)).Cast<TAttr>().ToArray();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TAttr[] AllAttributes<TAttr>(this MemberInfo mi)
{
return mi.AllAttributes(typeof(TAttr)).Cast<TAttr>().ToArray();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TAttr[] AllAttributes<TAttr>(this FieldInfo fi)
{
return fi.AllAttributes(typeof(TAttr)).Cast<TAttr>().ToArray();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TAttr[] AllAttributes<TAttr>(this PropertyInfo pi)
{
return pi.AllAttributes(typeof(TAttr)).Cast<TAttr>().ToArray();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static IEnumerable<T> GetRuntimeAttributes<T>(this Type type)
{
List<Attribute> attrs;
return typeAttributesMap.TryGetValue(type, out attrs)
? attrs.OfType<T>()
: new List<T>();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static IEnumerable<Attribute> GetRuntimeAttributes(this Type type, Type attrType = null)
{
List<Attribute> attrs;
return typeAttributesMap.TryGetValue(type, out attrs)
? attrs.Where(x => attrType == null || attrType.IsInstanceOf(x.GetType()))
: new List<Attribute>();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TAttr[] AllAttributes<TAttr>(this Type type)
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
where TAttr : Attribute
#endif
{
#if (NETFX_CORE || PCL)
return type.GetTypeInfo().GetCustomAttributes<TAttr>(true).ToArray();
#elif NETSTANDARD1_1
return type.GetTypeInfo().GetCustomAttributes<TAttr>(true)
.Union(type.GetRuntimeAttributes<TAttr>())
.ToArray();
#else
return type.GetCustomAttributes(typeof(TAttr), true)
.OfType<TAttr>()
.Union(type.GetRuntimeAttributes<TAttr>())
.ToArray();
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TAttr FirstAttribute<TAttr>(this Type type) where TAttr : class
{
#if (NETFX_CORE || PCL )
return (TAttr)type.GetTypeInfo().GetCustomAttributes(typeof(TAttr), true)
.Cast<TAttr>()
.FirstOrDefault();
#elif NETSTANDARD1_1
return (TAttr)type.GetTypeInfo().GetCustomAttributes(typeof(TAttr), true)
.Cast<TAttr>()
.FirstOrDefault()
?? type.GetRuntimeAttributes<TAttr>().FirstOrDefault();
#else
return (TAttr)type.GetCustomAttributes(typeof(TAttr), true)
.FirstOrDefault()
?? type.GetRuntimeAttributes<TAttr>().FirstOrDefault();
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TAttribute FirstAttribute<TAttribute>(this MemberInfo memberInfo)
{
return memberInfo.AllAttributes<TAttribute>().FirstOrDefault();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TAttribute FirstAttribute<TAttribute>(this ParameterInfo paramInfo)
{
return paramInfo.AllAttributes<TAttribute>().FirstOrDefault();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TAttribute FirstAttribute<TAttribute>(this PropertyInfo propertyInfo)
{
return propertyInfo.AllAttributes<TAttribute>().FirstOrDefault();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Type FirstGenericTypeDefinition(this Type type)
{
var genericType = type.FirstGenericType();
return genericType != null ? genericType.GetGenericTypeDefinition() : null;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsDynamic(this Assembly assembly)
{
#if __IOS__ || WP || NETFX_CORE || PCL || NETSTANDARD1_1
return false;
#else
try
{
var isDyanmic = assembly is System.Reflection.Emit.AssemblyBuilder
|| string.IsNullOrEmpty(assembly.Location);
return isDyanmic;
}
catch (NotSupportedException)
{
//Ignore assembly.Location not supported in a dynamic assembly.
return true;
}
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static MethodInfo GetStaticMethod(this Type type, string methodName, Type[] types = null)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
foreach (MethodInfo method in type.GetTypeInfo().DeclaredMethods)
{
if (method.IsStatic && method.Name == methodName)
{
return method;
}
}
return null;
#else
return types == null
? type.GetMethod(methodName, BindingFlags.Public | BindingFlags.Static)
: type.GetMethod(methodName, BindingFlags.Public | BindingFlags.Static, null, types, null);
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static MethodInfo GetMethodInfo(this Type type, string methodName, Type[] types = null)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return type.GetRuntimeMethods().FirstOrDefault(p => p.Name.Equals(methodName));
#else
return types == null
? type.GetMethod(methodName)
: type.GetMethod(methodName, types);
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static object InvokeMethod(this Delegate fn, object instance, object[] parameters = null)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return fn.GetMethodInfo().Invoke(instance, parameters ?? new object[] { });
#else
return fn.Method.Invoke(instance, parameters ?? new object[] { });
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static FieldInfo GetPublicStaticField(this Type type, string fieldName)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return type.GetRuntimeField(fieldName);
#else
return type.GetField(fieldName, BindingFlags.Public | BindingFlags.Static);
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Delegate MakeDelegate(this MethodInfo mi, Type delegateType, bool throwOnBindFailure = true)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return mi.CreateDelegate(delegateType);
#else
return Delegate.CreateDelegate(delegateType, mi, throwOnBindFailure);
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Type[] GenericTypeArguments(this Type type)
{
#if (NETFX_CORE || PCL)
return type.GenericTypeArguments;
#else
return type.GetGenericArguments();
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ConstructorInfo[] DeclaredConstructors(this Type type)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return type.GetTypeInfo().DeclaredConstructors.ToArray();
#else
return type.GetConstructors();
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool AssignableFrom(this Type type, Type fromType)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return type.GetTypeInfo().IsAssignableFrom(fromType.GetTypeInfo());
#else
return type.IsAssignableFrom(fromType);
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsStandardClass(this Type type)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
var typeInfo = type.GetTypeInfo();
return typeInfo.IsClass && !typeInfo.IsAbstract && !typeInfo.IsInterface;
#else
return type.IsClass && !type.IsAbstract && !type.IsInterface;
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsAbstract(this Type type)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return type.GetTypeInfo().IsAbstract;
#else
return type.IsAbstract;
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static PropertyInfo GetPropertyInfo(this Type type, string propertyName)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return type.GetRuntimeProperty(propertyName);
#else
return type.GetProperty(propertyName);
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static FieldInfo GetFieldInfo(this Type type, string fieldName)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return type.GetRuntimeField(fieldName);
#else
return type.GetField(fieldName);
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static FieldInfo[] GetWritableFields(this Type type)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return type.GetRuntimeFields().Where(p => !p.IsPublic && !p.IsStatic).ToArray();
#else
return type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.SetField);
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static MethodInfo SetMethod(this PropertyInfo pi, bool nonPublic = true)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return pi.SetMethod;
#else
return pi.GetSetMethod(nonPublic);
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static MethodInfo GetMethodInfo(this PropertyInfo pi, bool nonPublic = true)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return pi.GetMethod;
#else
return pi.GetGetMethod(nonPublic);
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool InstanceOfType(this Type type, object instance)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return instance.GetType().IsInstanceOf(type);
#else
return type.IsInstanceOfType(instance);
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsAssignableFromType(this Type type, Type fromType)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return type.GetTypeInfo().IsAssignableFrom(fromType.GetTypeInfo());
#else
return type.IsAssignableFrom(fromType);
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsClass(this Type type)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return type.GetTypeInfo().IsClass;
#else
return type.IsClass;
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsEnum(this Type type)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return type.GetTypeInfo().IsEnum;
#else
return type.IsEnum;
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsEnumFlags(this Type type)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return type.GetTypeInfo().IsEnum && type.FirstAttribute<FlagsAttribute>() != null;
#else
return type.IsEnum && type.FirstAttribute<FlagsAttribute>() != null;
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsUnderlyingEnum(this Type type)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
return type.GetTypeInfo().IsEnum;
#else
return type.IsEnum || type.UnderlyingSystemType.IsEnum;
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static MethodInfo[] GetMethodInfos(this Type type)
{
#if (NETFX_CORE || PCL || NETSTANDARD1_1)