1
1
using System ;
2
2
using System . Collections . Generic ;
3
+ using System . ComponentModel ;
3
4
using System . Reflection ;
4
5
using System . Runtime . CompilerServices ;
5
6
using System . Text ;
@@ -502,24 +503,6 @@ public static Type GetMemberType([NotNull] this MemberInfo memberInfo)
502
503
_ => throw new InvalidOperationException ( )
503
504
} ;
504
505
}
505
-
506
- /// <summary>
507
- /// Checks if <paramref name="type"/> is an anonymous type.
508
- /// </summary>
509
- /// <param name="type">Type to check.</param>
510
- /// <returns>True, if <paramref name="type"/> is an anonymous type.</returns>
511
- [ Pure ]
512
- public static bool IsAnonymous ( [ NotNull ] this Type type )
513
- {
514
- Code . NotNull ( type , nameof ( type ) ) ;
515
-
516
- return ! type . GetIsPublic ( )
517
- && type . GetIsGenericType ( )
518
- && ( type . Name . StartsWith ( "<>f__AnonymousType" , StringComparison . Ordinal )
519
- || type . Name . StartsWith ( "VB$AnonymousType" , StringComparison . Ordinal ) )
520
- && type . GetIsDefined ( typeof ( CompilerGeneratedAttribute ) , false ) ;
521
- }
522
-
523
506
/// <summary>
524
507
/// Returns default constructor.
525
508
/// </summary>
@@ -587,5 +570,88 @@ public static Type GetItemType([CanBeNull] this Type type)
587
570
type = type . GetBaseType ( ) ;
588
571
}
589
572
}
573
+
574
+ /// <summary>
575
+ /// Checks if <paramref name="type"/> is an anonymous type.
576
+ /// </summary>
577
+ /// <param name="type">Type to check.</param>
578
+ /// <returns>True, if <paramref name="type"/> is an anonymous type.</returns>
579
+ [ Pure ]
580
+ public static bool IsAnonymous ( [ NotNull ] this Type type )
581
+ {
582
+ Code . NotNull ( type , nameof ( type ) ) ;
583
+
584
+ return ! type . GetIsPublic ( )
585
+ && type . GetIsGenericType ( )
586
+ && ( type . Name . StartsWith ( "<>f__AnonymousType" , StringComparison . Ordinal )
587
+ || type . Name . StartsWith ( "VB$AnonymousType" , StringComparison . Ordinal ) )
588
+ && type . GetIsDefined ( typeof ( CompilerGeneratedAttribute ) , false ) ;
589
+ }
590
+
591
+ /// <summary>
592
+ /// Checks if <typeparamref name="TAttribute"/> is defined on <paramref name="type"/>.
593
+ /// </summary>
594
+ /// <param name="type">Type to check.</param>
595
+ /// <returns>True, if <typeparamref name="TAttribute"/> is defined on <paramref name="type"/></returns>
596
+ [ Pure ]
597
+ public static bool IsDefined < TAttribute > ( [ NotNull ] this Type type ) where TAttribute : Attribute
598
+ {
599
+ Code . NotNull ( type , nameof ( type ) ) ;
600
+
601
+ return type . IsDefined ( typeof ( TAttribute ) ) ;
602
+ }
603
+
604
+ /// <summary>
605
+ /// Checks if <typeparamref name="TAttribute"/> is defined on <paramref name="member"/>.
606
+ /// </summary>
607
+ /// <param name="member">Member to check.</param>
608
+ /// <returns>True, if <typeparamref name="TAttribute"/> is defined on <paramref name="member"/></returns>
609
+ [ Pure ]
610
+ public static bool IsDefined < TAttribute > ( [ NotNull ] this MemberInfo member ) where TAttribute : Attribute
611
+ {
612
+ Code . NotNull ( member , nameof ( member ) ) ;
613
+
614
+ return member . IsDefined ( typeof ( TAttribute ) ) ;
615
+ }
616
+
617
+ /// <summary>
618
+ /// Checks if <paramref name="type"/> is compiler generated type.
619
+ /// </summary>
620
+ /// <param name="type">Type to check.</param>
621
+ /// <returns>True, if <paramref name="type"/> is generated by compiler.</returns>
622
+ [ Pure ]
623
+ public static bool IsCompilerGenerated ( [ NotNull ] this Type type ) => type . IsDefined < CompilerGeneratedAttribute > ( ) ;
624
+
625
+ /// <summary>
626
+ /// Checks if <paramref name="member"/> is is compiler generated member.
627
+ /// </summary>
628
+ /// <param name="member">Member to check.</param>
629
+ /// <returns>True, if <paramref name="member"/> is generated by compiler.</returns>
630
+ [ Pure ]
631
+ public static bool IsCompilerGenerated ( [ NotNull ] this MemberInfo member ) => member . IsDefined < CompilerGeneratedAttribute > ( ) ;
632
+
633
+ #if TARGETS_NET || NETSTANDARD20_OR_GREATER || NETCOREAPP20_OR_GREATER // PUBLIC_API_CHANGES
634
+ /// <summary>
635
+ /// Checks if <paramref name="type"/> has no <see cref="BrowsableAttribute"/> defined
636
+ /// -or-
637
+ /// <see cref="BrowsableAttribute.Browsable"/> equals to <c>true</c>.
638
+ /// </summary>
639
+ /// <param name="type">Type to check.</param>
640
+ /// <returns>True, if <paramref name="type"/> is browsable.</returns>
641
+ [ Pure ]
642
+ public static bool IsBrowsable ( [ NotNull ] this Type type ) =>
643
+ type . GetCustomAttribute < BrowsableAttribute > ( ) ? . Browsable ?? true ;
644
+
645
+ /// <summary>
646
+ /// Checks if <paramref name="member"/> has no <see cref="BrowsableAttribute"/> defined
647
+ /// -or-
648
+ /// <see cref="BrowsableAttribute.Browsable"/> equals to <c>true</c>.
649
+ /// </summary>
650
+ /// <param name="member">Member to check.</param>
651
+ /// <returns>True, if <paramref name="member"/> is browsable.</returns>
652
+ [ Pure ]
653
+ public static bool IsBrowsable ( [ NotNull ] this MemberInfo member ) =>
654
+ member . GetCustomAttribute < BrowsableAttribute > ( ) ? . Browsable ?? true ;
655
+ #endif
590
656
}
591
657
}
0 commit comments