-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathObject.cs
2463 lines (2202 loc) · 99.7 KB
/
Object.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
#region Related components
using System;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Diagnostics;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.Dynamic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
#endregion
namespace net.vieapps.Components.Utility
{
/// <summary>
/// Static servicing methods for working with objects
/// </summary>
public static partial class ObjectService
{
#region Meta of objects
/// <summary>
/// Presents meta information of object's field/property/method
/// </summary>
[Serializable, DebuggerDisplay("Name = {Name}, IsPublic = {IsPublic}, IsStatic = {IsStatic}")]
public class MetaInfo
{
internal readonly bool? _isPublic, _isStatic;
/// <summary>
/// Initializes information of an object' meta data
/// </summary>
public MetaInfo() : this(null) { }
/// <summary>
/// Initializes information of an object' meta data
/// </summary>
/// <param name="info"></param>
/// <param name="isPublic"></param>
/// <param name="isStatic"></param>
public MetaInfo(MemberInfo info, bool? isPublic = null, bool? isStatic = null)
{
this.Info = info;
if (isPublic != null && isPublic.Value)
this._isPublic = true;
if (isStatic != null && isStatic.Value)
this._isStatic = true;
}
/// <summary>
/// Gets the detail information
/// </summary>
public MemberInfo Info { get; }
/// <summary>
/// Gets the name
/// </summary>
public string Name => this.Info?.Name;
/// <summary>
/// Gets the detail information (when this is meta information of a field)
/// </summary>
public FieldInfo FieldInfo => this.Info is FieldInfo fieldInfo ? fieldInfo : null;
/// <summary>
/// Specifies this meta is information of a field
/// </summary>
public bool IsField => this.FieldInfo != null;
/// <summary>
/// Gets the detail information (when this is meta information of a property)
/// </summary>
public PropertyInfo PropertyInfo => this.Info is PropertyInfo propertyInfo ? propertyInfo : null;
/// <summary>
/// Specifies this meta is information of a property
/// </summary>
public bool IsProperty => this.PropertyInfo != null;
/// <summary>
/// Gets the detail information (when this is meta information of a method)
/// </summary>
public MethodInfo MethodInfo => this.Info is MethodInfo methodInfo ? methodInfo : null;
/// <summary>
/// Specifies this meta is information of a method
/// </summary>
public bool IsMethod => this.MethodInfo != null;
/// <summary>
/// Specifies is public or not
/// </summary>
public bool IsPublic => (this.IsField && this.FieldInfo.IsPublic) || (this.IsMethod && this.MethodInfo.IsPublic) || (this._isPublic != null && this._isPublic.Value);
/// <summary>
/// Specifies is static or not
/// </summary>
public bool IsStatic => (this.IsField && this.FieldInfo.IsStatic) || (this.IsMethod && this.MethodInfo.IsStatic) || (this._isStatic != null && this._isStatic.Value);
/// <summary>
/// Gets the type of the member
/// </summary>
public Type Type => this.FieldInfo?.FieldType ?? this.PropertyInfo?.PropertyType ?? this.MethodInfo?.ReturnType;
}
/// <summary>
/// Presents information of an object's attribute (field or property)
/// </summary>
[Serializable, DebuggerDisplay("Name = {Name}, IsPublic = {IsPublic}, CanRead = {CanRead}, CanWrite = {CanWrite}")]
public class AttributeInfo : MetaInfo
{
/// <summary>
/// Initializes information of an objects' attribute
/// </summary>
public AttributeInfo() : base(null) { }
/// <summary>
/// Initializes information of an objects' attribute
/// </summary>
/// <param name="info"></param>
/// <param name="isPublic"></param>
/// <param name="isStatic"></param>
public AttributeInfo(MemberInfo info, bool? isPublic = null, bool? isStatic = null) : base(info, isPublic, isStatic) { }
/// <summary>
/// Specifies this attribute can be read or not
/// </summary>
public bool CanRead => !this.IsMethod && (this.IsField || this.PropertyInfo.CanRead);
/// <summary>
/// Specifies this attribute can be written or not
/// </summary>
public bool CanWrite => !this.IsMethod && (this.IsField || this.PropertyInfo.CanWrite);
}
static ConcurrentDictionary<Type, List<MetaInfo>> TypeMeta { get; } = new ConcurrentDictionary<Type, List<MetaInfo>>();
/// <summary>
/// Gets the meta information of a specified type
/// </summary>
/// <param name="type"></param>
/// <param name="predicate"></param>
/// <returns></returns>
public static List<MetaInfo> GetMetaInfo(this Type type, Func<MetaInfo, bool> predicate = null)
{
if (type == null || !type.IsClassType())
return new List<MetaInfo>();
if (!ObjectService.TypeMeta.TryGetValue(type, out var metaInfo) || metaInfo == null)
lock (ObjectService.TypeMeta)
{
if (!ObjectService.TypeMeta.TryGetValue(type, out metaInfo) || metaInfo == null)
{
metaInfo = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static).Where(field => !field.Name.StartsWith("<")).Select(field => new MetaInfo(field))
.Concat(type.GetProperties(BindingFlags.Instance | BindingFlags.Public).Select(property => new MetaInfo(property, true)))
.Concat(type.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic).Select(property => new MetaInfo(property, false)))
.Concat(type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static).Where(method => !method.Name.StartsWith("get_") && !method.Name.StartsWith("set_") && !method.Name.StartsWith("add_") && !method.Name.StartsWith("remove_")).Select(method => new MetaInfo(method)))
.ToList();
metaInfo = metaInfo
.Concat(type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static).Where(property => metaInfo.FirstOrDefault(info => info.Name == property.Name) == null).Select(property => new MetaInfo(property, true, true)))
.Concat(type.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static).Where(property => metaInfo.FirstOrDefault(info => info.Name == property.Name) == null).Select(property => new MetaInfo(property, false, true)))
.ToList();
ObjectService.TypeMeta.TryAdd(type, metaInfo);
}
}
return predicate != null
? metaInfo.Where(info => predicate(info)).ToList()
: metaInfo;
}
/// <summary>
/// Gets the meta information of a specified type
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="predicate"></param>
/// <returns></returns>
public static List<MetaInfo> GetMetaInfo<T>(Func<MetaInfo, bool> predicate = null) where T : class
=> ObjectService.GetMetaInfo(typeof(T), predicate);
/// <summary>
/// Gets the meta information of this object
/// </summary>
/// <param name="object"></param>
/// <param name="predicate"></param>
/// <returns></returns>
/// <returns></returns>
public static List<MetaInfo> GetMetaInfo(this object @object, Func<MetaInfo, bool> predicate = null)
=> ObjectService.GetMetaInfo(@object?.GetType(), predicate);
/// <summary>
/// Gets the collection of fields
/// </summary>
/// <param name="type">The type for processing</param>
/// <param name="predicate">The predicate</param>
/// <returns>Collection of private fields/attributes</returns>
public static List<MetaInfo> GetFields(Type type, Func<MetaInfo, bool> predicate = null)
=> type != null && type.IsClassType()
? predicate != null
? type.GetMetaInfo(info => info.IsField).Where(info => predicate(info)).ToList()
: type.GetMetaInfo(info => info.IsField).ToList()
: new List<MetaInfo>();
/// <summary>
/// Gets the collection of fields
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="predicate"></param>
/// <returns></returns>
public static List<MetaInfo> GetFields<T>(Func<MetaInfo, bool> predicate = null) where T : class
=> ObjectService.GetFields(typeof(T), predicate);
/// <summary>
/// Gets the collection of fields
/// </summary>
/// <param name="object"></param>
/// <param name="predicate"></param>
/// <returns></returns>
/// <returns></returns>
public static List<MetaInfo> GetFields(this object @object, Func<MetaInfo, bool> predicate = null)
=> ObjectService.GetFields(@object?.GetType(), predicate);
/// <summary>
/// Gets the collection of properties
/// </summary>
/// <param name="type"></param>
/// <param name="predicate"></param>
/// <returns></returns>
public static List<MetaInfo> GetProperties(this Type type, Func<MetaInfo, bool> predicate = null)
=> type != null && type.IsClassType()
? predicate != null
? type.GetMetaInfo(info => info.IsProperty).Where(info => predicate(info)).ToList()
: type.GetMetaInfo(info => info.IsProperty).ToList()
: new List<MetaInfo>();
/// <summary>
/// Gets the collection of properties
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="predicate"></param>
/// <returns></returns>
public static List<MetaInfo> GetProperties<T>(Func<MetaInfo, bool> predicate = null) where T : class
=> ObjectService.GetProperties(typeof(T), predicate);
/// <summary>
/// Gets the collection of properties
/// </summary>
/// <param name="object"></param>
/// <param name="predicate"></param>
/// <returns></returns>
public static List<MetaInfo> GetProperties(this object @object, Func<MetaInfo, bool> predicate = null)
=> ObjectService.GetProperties(@object?.GetType(), predicate);
/// <summary>
/// Gets the collection of methods
/// </summary>
/// <param name="type"></param>
/// <param name="predicate"></param>
/// <returns></returns>
public static List<MetaInfo> GetMethods(this Type type, Func<MetaInfo, bool> predicate = null)
=> type != null && type.IsClassType()
? predicate != null
? type.GetMetaInfo().Where(info => info.IsMethod).Where(info => predicate(info)).ToList()
: type.GetMetaInfo().Where(info => info.IsMethod).ToList()
: new List<MetaInfo>();
/// <summary>
/// Gets the collection of methods
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="predicate"></param>
/// <returns></returns>
public static List<MetaInfo> GetMethods<T>(Func<MetaInfo, bool> predicate = null) where T : class
=> ObjectService.GetMethods(typeof(T), predicate);
/// <summary>
/// Gets the collection of methods
/// </summary>
/// <param name="object"></param>
/// <param name="predicate"></param>
/// <returns></returns>
public static List<MetaInfo> GetMethods(this object @object, Func<MetaInfo, bool> predicate = null)
=> ObjectService.GetMethods(@object?.GetType(), predicate);
/// <summary>
/// Gets the collection of all attributes (means all fields and all properties)
/// </summary>
/// <param name="type"></param>
/// <param name="predicate"></param>
/// <returns></returns>
public static List<AttributeInfo> GetAttributes(this Type type, Func<AttributeInfo, bool> predicate = null)
=> type != null && type.IsClassType()
? predicate != null
? type.GetMetaInfo(info => info.IsField || info.IsProperty).Select(info => new AttributeInfo(info.Info, info._isPublic, info._isStatic)).Where(info => predicate(info)).ToList()
: type.GetMetaInfo(info => info.IsField || info.IsProperty).Select(info => new AttributeInfo(info.Info, info._isPublic, info._isStatic)).ToList()
: new List<AttributeInfo>();
/// <summary>
/// Gets the collection of all attributes (means all fields and all properties)
/// </summary>
/// <param name="object"></param>
/// <param name="predicate"></param>
/// <returns></returns>
public static List<AttributeInfo> GetAttributes(this object @object, Func<AttributeInfo, bool> predicate = null)
=> ObjectService.GetAttributes(@object?.GetType(), predicate);
/// <summary>
/// Gets the collection of public attributes (means public fields and public properties)
/// </summary>
/// <param name="type"></param>
/// <param name="predicate"></param>
/// <returns></returns>
public static List<AttributeInfo> GetPublicAttributes(this Type type, Func<AttributeInfo, bool> predicate = null)
=> ObjectService.GetAttributes(type, predicate).Where(info => info.IsPublic).ToList();
/// <summary>
/// Gets the collection of public attributes (means public fields and public properties)
/// </summary>
/// <param name="object"></param>
/// <param name="predicate"></param>
/// <returns></returns>
public static List<AttributeInfo> GetPublicAttributes(this object @object, Func<AttributeInfo, bool> predicate = null)
=> ObjectService.GetPublicAttributes(@object?.GetType(), predicate);
/// <summary>
/// Gets the collection of private attributes (means private fields and private properties)
/// </summary>
/// <param name="type"></param>
/// <param name="predicate"></param>
/// <returns></returns>
public static List<AttributeInfo> GetPrivateAttributes(this Type type, Func<AttributeInfo, bool> predicate = null)
=> ObjectService.GetAttributes(type, predicate).Where(info => !info.IsPublic).ToList();
/// <summary>
/// Gets the collection of private attributes (means private fields and private properties)
/// </summary>
/// <param name="object"></param>
/// <param name="predicate"></param>
/// <returns></returns>
public static List<AttributeInfo> GetPrivateAttributes(this object @object, Func<AttributeInfo, bool> predicate = null)
=> ObjectService.GetPrivateAttributes(@object?.GetType(), predicate);
/// <summary>
/// Gets a static attribute (means static field or static property) of the type that specified by name
/// </summary>
/// <param name="type">The type of the static class that contains the static object</param>
/// <param name="name">The name of the static attribute</param>
/// <returns></returns>
public static object GetStaticObject(this Type type, string name)
{
try
{
var attribute = type != null && !string.IsNullOrWhiteSpace(name) ? type.GetAttributes(attr => attr.IsStatic && attr.CanRead).FirstOrDefault(attr => attr.Name.IsEquals(name)) : null;
return attribute?.FieldInfo?.GetValue(null) ?? attribute?.PropertyInfo?.GetValue(null);
}
catch
{
return null;
}
}
/// <summary>
/// Get the full type name (type name with assembly name) of this type
/// </summary>
/// <param name="type"></param>
/// <param name="justName">true to get only name (means last element in full namespace)</param>
/// <returns>The string that presents type name</returns>
public static string GetTypeName(this Type type, bool justName = false)
{
if (type == null)
return "";
if (!justName)
return type.FullName + "," + type.Assembly.GetName().Name;
if (!type.IsGenericType)
return type.FullName.ToArray('.').Last();
var typeName = type.FullName;
var pos = typeName.IndexOf("[");
typeName = typeName.Remove(pos + 1, typeName.LastIndexOf("]") - pos - 1);
typeName = typeName.Insert(pos + 1, type.GetGenericArguments().Select(gtype => gtype.GetTypeName(true)).Join(","));
typeName = typeName.ToArray('.').Last();
pos = typeName.IndexOf("`");
return typeName.Remove(pos, typeName.IndexOf("[") - pos).Replace("[", "<").Replace("]", ">");
}
/// <summary>
/// Get the full type name (type name with assembly name) of this object
/// </summary>
/// <param name="object"></param>
/// <param name="justName">true to get only name (means last element in full namespace)</param>
/// <returns></returns>
public static string GetTypeName(this object @object, bool justName = false)
=> @object?.GetType().GetTypeName(justName);
/// <summary>
/// Gets the collection of custom attributes
/// </summary>
/// <param name="type"></param>
/// <param name="inherit">true to search this member's inheritance chain to find the attributes (default is true)</param>
/// <returns>The collection of custom attributes</returns>
public static List<T> GetCustomAttributes<T>(this Type type, bool inherit = true) where T : class
=> type?.GetCustomAttributes(typeof(T), inherit).Select(attr => attr as T).ToList();
/// <summary>
/// Gets the collection of custom attributes
/// </summary>
/// <param name="attribute"></param>
/// <param name="inherit">true to search this member's inheritance chain to find the attributes (default is true)</param>
/// <returns>The collection of custom attributes</returns>
public static List<T> GetCustomAttributes<T>(this AttributeInfo attribute, bool inherit = true) where T : class
=> attribute?.Info?.GetCustomAttributes(typeof(T), inherit).Select(attr => attr as T).ToList();
/// <summary>
/// Gets the collection of custom attributes
/// </summary>
/// <param name="object"></param>
/// <param name="inherit">true to search this member's inheritance chain to find the attributes (default is true)</param>
/// <returns>The collection of custom attributes</returns>
public static List<T> GetCustomAttributes<T>(this object @object, bool inherit = true) where T : class
=> @object?.GetType().GetCustomAttributes<T>(inherit);
/// <summary>
/// Gets the first custom attribute
/// </summary>
/// <param name="type"></param>
/// <param name="inherit">true to search this member's inheritance chain to find the attributes (default is true)</param>
/// <returns>The first custom attribute</returns>
public static T GetCustomAttribute<T>(this Type type, bool inherit = true) where T : class
=> type?.GetCustomAttributes<T>(inherit).FirstOrDefault();
/// <summary>
/// Gets the first custom attribute
/// </summary>
/// <param name="attribute"></param>
/// <param name="inherit">true to search this member's inheritance chain to find the attributes (default is true)</param>
/// <returns>The first custom attribute</returns>
public static T GetCustomAttribute<T>(this AttributeInfo attribute, bool inherit = true) where T : class
=> attribute?.GetCustomAttributes<T>(inherit).FirstOrDefault();
/// <summary>
/// Gets the first custom attribute
/// </summary>
/// <param name="object"></param>
/// <param name="inherit">true to search this member's inheritance chain to find the attributes (default is true)</param>
/// <returns>The first custom attribute</returns>
public static T GetCustomAttribute<T>(this object @object, bool inherit = true) where T : class
=> @object?.GetType().GetCustomAttribute<T>(inherit);
internal static AsArrayAttribute GetAsArrayAttribute(this AttributeInfo attribute)
=> attribute?.GetCustomAttribute<AsArrayAttribute>();
internal static AsObjectAttribute GetAsObjectAttribute(this AttributeInfo attribute)
=> attribute?.GetCustomAttribute<AsObjectAttribute>();
internal static List<AttributeInfo> GetSpecialSerializeAttributes(this Type type)
=> type?.GetPublicAttributes(attribute => !attribute.IsStatic)
.Where(attribute => (attribute.IsGenericDictionaryOrCollection() && attribute.GetAsArrayAttribute() != null) || (attribute.IsGenericListOrHashSet() && attribute.GetAsObjectAttribute() != null))
.ToList() ?? new List<AttributeInfo>();
internal static List<AttributeInfo> GetSpecialSerializeAttributes(this object @object)
=> @object?.GetType().GetSpecialSerializeAttributes();
internal static bool GotSpecialSerializeAttributes(this Type type)
=> type != null && type.IsClassType() && type.GetSpecialSerializeAttributes().Any();
/// <summary>
/// Gets the state that indicates the custom attribute is defined or not
/// </summary>
/// <param name="type"></param>
/// <param name="inherit">true to search this member's inheritance chain to find the attributes</param>
/// <returns>The first custom attribute</returns>
public static bool IsDefined<T>(this Type type, bool inherit) where T : class
=> type != null && type.IsDefined(typeof(T), inherit);
/// <summary>
/// Gets the state that indicates the custom attribute is defined or not
/// </summary>
/// <param name="attribute"></param>
/// <param name="inherit">true to search this member's inheritance chain to find the attributes</param>
/// <returns>The first custom attribute</returns>
public static bool IsDefined<T>(this AttributeInfo attribute, bool inherit) where T : class
=> attribute != null && attribute.Type != null && attribute.Type.IsDefined<T>(inherit);
/// <summary>
/// Gets the state that indicates the custom attribute is defined or not
/// </summary>
/// <param name="object"></param>
/// <param name="inherit">true to search this member's inheritance chain to find the attributes</param>
/// <returns>The first custom attribute</returns>
public static bool IsDefined<T>(this object @object, bool inherit) where T : class
=> @object != null && @object.GetType().IsDefined<T>(inherit);
/// <summary>
/// Gets the state to determines the type is primitive or not
/// </summary>
/// <param name="type"></param>
/// <returns>true if type is primitive</returns>
public static bool IsPrimitiveType(this Type type)
=> type != null && (type.IsPrimitive || type.IsStringType() || type.IsDateTimeType() || type.IsNumericType());
/// <summary>
/// Gets the state to determines the attribute's type is primitive or not
/// </summary>
/// <param name="attribute"></param>
/// <returns>true if type is numeric</returns>
public static bool IsPrimitiveType(this AttributeInfo attribute)
=> attribute != null && attribute.Type != null && attribute.Type.IsPrimitiveType();
/// <summary>
/// Gets the state to determines the object's type is primitive or not
/// </summary>
/// <param name="object"></param>
/// <returns>true if type is numeric</returns>
public static bool IsPrimitiveType(this object @object)
=> @object != null && @object.GetType().IsPrimitiveType();
/// <summary>
/// Gets the state to determines the type is string or not
/// </summary>
/// <param name="type"></param>
/// <returns>true if type is string</returns>
public static bool IsStringType(this Type type)
=> type != null && type.Equals(typeof(string));
/// <summary>
/// Gets the state to determines the attribute's type is string or not
/// </summary>
/// <param name="attribute"></param>
/// <returns>true if type is numeric</returns>
public static bool IsStringType(this AttributeInfo attribute)
=> attribute != null && attribute.Type != null && attribute.Type.IsStringType();
/// <summary>
/// Gets the state to determines the object's type is string or not
/// </summary>
/// <param name="object"></param>
/// <returns>true if type is numeric</returns>
public static bool IsStringType(this object @object)
=> @object != null && @object.GetType().IsStringType();
/// <summary>
/// Gets the state to determines the type is date-time or not
/// </summary>
/// <param name="type"></param>
/// <returns>true if type is date-time</returns>
public static bool IsDateTimeType(this Type type)
=> type != null && (type.Equals(typeof(DateTime)) || type.Equals(typeof(DateTimeOffset)) || type.Equals(typeof(DateTime?)) || type.Equals(typeof(DateTimeOffset?)));
/// <summary>
/// Gets the state to determines the attribute's type is date-time or not
/// </summary>
/// <param name="attribute"></param>
/// <returns>true if type is numeric</returns>
public static bool IsDateTimeType(this AttributeInfo attribute)
=> attribute != null && attribute.Type != null && attribute.Type.IsDateTimeType();
/// <summary>
/// Gets the state to determines the object's type is date-time or not
/// </summary>
/// <param name="object"></param>
/// <returns>true if type is numeric</returns>
public static bool IsDateTimeType(this object @object)
=> @object != null && @object.GetType().IsDateTimeType();
/// <summary>
/// Gets the state to determines the type is integral numeric or not
/// </summary>
/// <param name="type"></param>
/// <returns>true if type is integral numeric</returns>
public static bool IsIntegralType(this Type type)
=> type != null && (type.Equals(typeof(byte)) || type.Equals(typeof(sbyte))
|| type.Equals(typeof(byte?)) || type.Equals(typeof(sbyte?))
|| type.Equals(typeof(short)) || type.Equals(typeof(int)) || type.Equals(typeof(long))
|| type.Equals(typeof(short?)) || type.Equals(typeof(int?)) || type.Equals(typeof(long?))
|| type.Equals(typeof(ushort)) || type.Equals(typeof(uint)) || type.Equals(typeof(ulong))
|| type.Equals(typeof(ushort?)) || type.Equals(typeof(uint?)) || type.Equals(typeof(ulong?)));
/// <summary>
/// Gets the state to determines the attribute's type is integral numeric or not
/// </summary>
/// <param name="attribute"></param>
/// <returns>true if type is numeric</returns>
public static bool IsIntegralType(this AttributeInfo attribute)
=> attribute != null && attribute.Type != null && attribute.Type.IsIntegralType();
/// <summary>
/// Gets the state to determines the object's type is integral numeric or not
/// </summary>
/// <param name="object"></param>
/// <returns>true if type is numeric</returns>
public static bool IsIntegralType(this object @object)
=> @object != null && @object.GetType().IsIntegralType();
/// <summary>
/// Gets the state to determines the type is floating numeric or not
/// </summary>
/// <param name="type"></param>
/// <returns>true if type is floating numeric</returns>
public static bool IsFloatingPointType(this Type type)
=> type != null && (type.Equals(typeof(decimal)) || type.Equals(typeof(decimal?))
|| type.Equals(typeof(double)) || type.Equals(typeof(double?))
|| type.Equals(typeof(float)) || type.Equals(typeof(float?)));
/// <summary>
/// Gets the state to determines the attribute's type is floating numeric or not
/// </summary>
/// <param name="attribute"></param>
/// <returns>true if type is numeric</returns>
public static bool IsFloatingPointType(this AttributeInfo attribute)
=> attribute != null && attribute.Type != null && attribute.Type.IsFloatingPointType();
/// <summary>
/// Gets the state to determines the object's type is floating numeric or not
/// </summary>
/// <param name="object"></param>
/// <returns>true if type is numeric</returns>
public static bool IsFloatingPointType(this object @object)
=> @object != null && @object.GetType().IsFloatingPointType();
/// <summary>
/// Gets the state to determines the type is numeric or not
/// </summary>
/// <param name="type"></param>
/// <returns>true if type is numeric</returns>
public static bool IsNumericType(this Type type)
=> type != null && (type.IsIntegralType() || type.IsFloatingPointType());
/// <summary>
/// Gets the state to determines the attribute's type is numeric or not
/// </summary>
/// <param name="attribute"></param>
/// <returns>true if type is numeric</returns>
public static bool IsNumericType(this AttributeInfo attribute)
=> attribute != null && attribute.Type != null && attribute.Type.IsNumericType();
/// <summary>
/// Gets the state to determines the object's type is numeric or not
/// </summary>
/// <param name="object"></param>
/// <returns>true if type is numeric</returns>
public static bool IsNumericType(this object @object)
=> @object != null && @object.GetType().IsNumericType();
/// <summary>
/// Gets the state to determines the type is a reference of a class or not
/// </summary>
/// <param name="type"></param>
/// <returns>true if type is numeric</returns>
public static bool IsClassType(this Type type)
=> type != null && !type.IsPrimitiveType() && type.IsClass;
/// <summary>
/// Gets the state to determines the attribute's type is a reference of a class or not
/// </summary>
/// <param name="attribute"></param>
/// <returns>true if type is numeric</returns>
public static bool IsClassType(this AttributeInfo attribute)
=> attribute != null && attribute.Type != null && attribute.Type.IsClassType();
/// <summary>
/// Gets the state to determines the object's type is a reference of a class or not
/// </summary>
/// <param name="object"></param>
/// <returns>true if type is numeric</returns>
public static bool IsClassType(this object @object)
=> @object != null && @object.GetType().IsClassType();
/// <summary>
/// Gets the state to determines the attribute's type is enumeration or not
/// </summary>
/// <param name="attribute"></param>
/// <returns>true if type is numeric</returns>
public static bool IsEnum(this AttributeInfo attribute)
=> attribute != null && attribute.Type != null && attribute.Type.IsEnum;
/// <summary>
/// Gets the state to determines the object's type is enumeration or not
/// </summary>
/// <param name="object"></param>
/// <returns>true if type is numeric</returns>
public static bool IsEnum(this object @object)
=> @object != null && @object.GetType().IsEnum;
/// <summary>
/// Gets the state that determines the type is got Json.NET string enumeration or not
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static bool IsStringEnum(this Type type)
=> type != null && type.IsEnum && typeof(StringEnumConverter).Equals(type.GetCustomAttribute<JsonConverterAttribute>()?.ConverterType);
/// <summary>
/// Gets the state that determines the attribute's type is got Json.NET string enumeration or not
/// </summary>
/// <param name="attribute"></param>
/// <returns></returns>
public static bool IsStringEnum(this AttributeInfo attribute)
=> attribute != null && attribute.Type != null && attribute.Type.IsStringEnum();
/// <summary>
/// Gets the state to determines the object's type is got Json.NET string enumeration or not
/// </summary>
/// <param name="object"></param>
/// <returns>true if type is numeric</returns>
public static bool IsStringEnum(this object @object)
=> @object != null && @object.GetType().IsStringEnum();
#if NETSTANDARD2_0
/// <summary>
/// Gets the state to determines the attribute's type is serializable (got 'Serializable' attribute) or not
/// </summary>
/// <param name="attribute"></param>
/// <returns></returns>
public static bool IsSerializable(this AttributeInfo attribute)
=> attribute != null && attribute.Type != null && attribute.Type.IsSerializable;
/// <summary>
/// Gets the state to determines the object is serializable (got 'Serializable' attribute) or not
/// </summary>
/// <param name="object"></param>
/// <returns></returns>
public static bool IsSerializable(this object @object)
=> @object != null && @object.GetType().IsSerializable;
#endif
/// <summary>
/// Gets the nullable type from this type
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static Type GetNullableType(this Type type)
{
if (type == null)
return null;
type = Nullable.GetUnderlyingType(type) ?? type;
return type.IsValueType
? typeof(Nullable<>).MakeGenericType(type)
: type;
}
/// <summary>
/// Gets the nullable type from this type
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static Type GetNullableType<T>()
=> typeof(T).GetNullableType();
/// <summary>
/// Gets the state to determines this type is nullable or not
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static bool IsNullable(this Type type)
=> type != null && Nullable.GetUnderlyingType(type) != null;
/// <summary>
/// Gets the state to determines this attribute is nullable or not
/// </summary>
/// <param name="attribute"></param>
/// <returns></returns>
public static bool IsNullable(this AttributeInfo attribute)
=> attribute != null && attribute.Type != null && attribute.Type.IsNullable();
/// <summary>
/// Gets the state to determines this object is nullable or not
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="object"></param>
/// <returns></returns>
public static bool IsNullable<T>(this T @object)
=> @object != null && @object.GetType().IsNullable();
#endregion
#region Meta of collections
/// <summary>
/// Gets the state to determines this type is sub-class of a generic type
/// </summary>
/// <param name="type">The type for checking</param>
/// <param name="genericType">The generic type for checking</param>
/// <returns>true if the checking type is sub-class of the generic type</returns>
public static bool IsSubclassOfGeneric(this Type type, Type genericType)
{
if (type == null || genericType == null || !genericType.IsGenericType)
return false;
var baseType = type;
var rootType = typeof(object);
while (baseType != null && baseType != rootType)
{
var current = baseType.IsGenericType
? baseType.GetGenericTypeDefinition()
: baseType;
if (genericType == current)
return true;
baseType = baseType.BaseType;
}
return false;
}
/// <summary>
/// Gets the generic type arguments of this type
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static List<Type> GetGenericTypeArguments(this Type type)
=> type != null && type.IsGenericType ? type.GenericTypeArguments.ToList() : new List<Type>();
/// <summary>
/// Gets the generic type arguments of this attribute type
/// </summary>
/// <param name="attribute"></param>
/// <returns></returns>
public static List<Type> GetGenericTypeArguments(this AttributeInfo attribute)
=> attribute?.Type.GetGenericTypeArguments() ?? new List<Type>();
/// <summary>
/// Gets the generic type arguments of this object
/// </summary>
/// <param name="object">The type for processing</param>
/// <returns></returns>
public static List<Type> GetGenericTypeArguments(this object @object)
=> @object?.GetType().GetGenericTypeArguments() ?? new List<Type>();
/// <summary>
/// Gets the first element of the generic type arguments of this type
/// </summary>
/// <param name="type">The type for processing</param>
/// <returns></returns>
public static Type GetFirstGenericTypeArgument(this Type type)
=> type != null && type.IsGenericType ? type.GenericTypeArguments.First() : null;
/// <summary>
/// Gets the first element of the generic type arguments of this attribute
/// </summary>
/// <param name="attribute"></param>
/// <returns></returns>
public static Type GetFirstGenericTypeArgument(this AttributeInfo attribute)
=> attribute?.Type.GetFirstGenericTypeArgument();
/// <summary>
/// Gets the first element of the generic type arguments of this object
/// </summary>
/// <param name="object"></param>
/// <returns></returns>
public static Type GetFirstGenericTypeArgument(this object @object)
=> @object?.GetType().GetFirstGenericTypeArgument();
/// <summary>
/// Gets the last element of the generic type arguments of this type
/// </summary>
/// <param name="type">The type for processing</param>
/// <returns></returns>
public static Type GetLastGenericTypeArgument(this Type type)
=> type != null && type.IsGenericType ? type.GenericTypeArguments.Last() : null;
/// <summary>
/// Gets the last element of the generic type arguments of this attribute
/// </summary>
/// <param name="attribute"></param>
/// <returns></returns>
public static Type GetLastGenericTypeArgument(this AttributeInfo attribute)
=> attribute?.Type.GetLastGenericTypeArgument();
/// <summary>
/// Gets the last element of the generic type arguments of this object
/// </summary>
/// <param name="object"></param>
/// <returns></returns>
public static Type GetLastGenericTypeArgument(this object @object)
=> @object?.GetType().GetLastGenericTypeArgument();
/// <summary>
/// Gets the state to determines the type is a generic list
/// </summary>
/// <param name="type"></param>
/// <returns>true if the type is a generic list; otherwise false.</returns>
public static bool IsGenericList(this Type type)
=> type != null && type.IsGenericType && type.IsSubclassOfGeneric(typeof(List<>));
/// <summary>
/// Gets the state to determines the attribute's type is a generic list
/// </summary>
/// <param name="attribute">Teh attribute for checking</param>
/// <returns>true if the type is a generic list; otherwise false.</returns>
public static bool IsGenericList(this AttributeInfo attribute)
=> attribute != null && attribute.Type != null && attribute.Type.IsGenericList();
/// <summary>
/// Gets the state to determines the object's type is a generic list
/// </summary>
/// <param name="object">The object for checking type</param>
/// <returns>true if the type of the object is a reference (or sub-class) of a generic list; otherwise false.</returns>
public static bool IsGenericList(this object @object)
=> @object != null && @object.GetType().IsGenericList();
/// <summary>
/// Gets the state to determines the type is a generic hash-set
/// </summary>
/// <param name="type"></param>
/// <returns>true if the type is a reference (or sub-class) of a generic hash-set; otherwise false.</returns>
public static bool IsGenericHashSet(this Type type)
=> type != null && type.IsGenericType && type.IsSubclassOfGeneric(typeof(HashSet<>));
/// <summary>
/// Gets the state to determines the attribute's type is a generic hash-set
/// </summary>
/// <param name="attribute">Teh attribute for checking</param>
/// <returns>true if the type is a generic list; otherwise false.</returns>
public static bool IsGenericHashSet(this AttributeInfo attribute)
=> attribute != null && attribute.Type != null && attribute.Type.IsGenericHashSet();
/// <summary>
/// Gets the state to determines the object's type is a generic hash-set
/// </summary>
/// <param name="object">The object for checking type</param>
/// <returns>true if the type of the object is a reference (or sub-class) of a generic hash-set; otherwise false.</returns>
public static bool IsGenericHashSet(this object @object)
=> @object != null && @object.GetType().IsGenericHashSet();
/// <summary>
/// Gets the state to determines the type is a generic list or generic hash-set
/// </summary>
/// <param name="type"></param>
/// <returns>true if the type is a reference (or sub-class) of a generic list or generic hash-set; otherwise false.</returns>
public static bool IsGenericListOrHashSet(this Type type)
=> type != null && (type.IsGenericList() || type.IsGenericHashSet());
/// <summary>
/// Gets the state to determines the attribute's type is a generic list or generic hash-set
/// </summary>
/// <param name="attribute">Teh attribute for checking</param>
/// <returns>true if the type is a generic list; otherwise false.</returns>
public static bool IsGenericListOrHashSet(this AttributeInfo attribute)
=> attribute != null && attribute.Type != null && attribute.Type.IsGenericListOrHashSet();
/// <summary>
/// Gets the state to determines the object's type is generic list or generic hash-set
/// </summary>
/// <param name="object">The object for checking type</param>
/// <returns>true if the type of the object is a reference (or sub-class) of a generic list or generic hash-set; otherwise false.</returns>
public static bool IsGenericListOrHashSet(this object @object)
=> @object != null && @object.GetType().IsGenericListOrHashSet();
/// <summary>
/// Gets the state to determines the type is reference of a generic dictionary
/// </summary>
/// <param name="type"></param>
/// <returns>true if the type is a reference (or sub-class) of a generic dictionary; otherwise false.</returns>
public static bool IsGenericDictionary(this Type type)
=> type != null && type.IsGenericType && type.IsSubclassOfGeneric(typeof(Dictionary<,>));
/// <summary>
/// Gets the state to determines the attribute's type is a generic dictionary
/// </summary>
/// <param name="attribute">Teh attribute for checking</param>
/// <returns>true if the type is a generic list; otherwise false.</returns>
public static bool IsGenericDictionary(this AttributeInfo attribute)
=> attribute != null && attribute.Type != null && attribute.Type.IsGenericDictionary();
/// <summary>
/// Gets the state to determines the type of the object is reference of a generic dictionary
/// </summary>
/// <param name="object">The object for checking type</param>
/// <returns>true if the type of the object is a reference (or sub-class) of a generic dictionary; otherwise false.</returns>
public static bool IsGenericDictionary(this object @object)
=> @object != null && @object.GetType().IsGenericDictionary();
/// <summary>
/// Gets the state to determines the type is reference of a generic collection
/// </summary>
/// <param name="type"></param>
/// <returns>true if the type is sub-class of the generic collection class; otherwise false.</returns>
public static bool IsGenericCollection(this Type type)
=> type != null && type.IsGenericType && type.IsSubclassOfGeneric(typeof(Collection<,>));
/// <summary>
/// Gets the state to determines the attribute's type is a generic collection
/// </summary>
/// <param name="attribute">Teh attribute for checking</param>
/// <returns>true if the type is a generic list; otherwise false.</returns>
public static bool IsGenericCollection(this AttributeInfo attribute)
=> attribute != null && attribute.Type != null && attribute.Type.IsGenericCollection();
/// <summary>
/// Gets the state to determines the type of the object is reference of a generic collection