1+ using System . Runtime . CompilerServices ;
2+
3+ [ assembly: InternalsVisibleTo ( "Test" ) ]
4+
5+ namespace NetCore . AutoRegisterDi
6+ {
7+ using System ;
8+ using System . Collections . Generic ;
9+ using System . Linq ;
10+ using System . Reflection ;
11+ using Attributes ;
12+ using Microsoft . Extensions . DependencyInjection ;
13+
14+
15+ /// <summary>
16+ /// Extensions for <see cref="Type"/>
17+ /// </summary>
18+ internal static class TypeExtensions
19+ {
20+ /// <summary>
21+ /// Check if type marked by <see cref="DoNotAutoRegisterAttribute"/>
22+ /// </summary>
23+ /// <param name="type">type</param>
24+ public static bool IsIgnoredType ( this Type type )
25+ {
26+ var doNotAutoRegisterAttribute = type . GetCustomAttribute < DoNotAutoRegisterAttribute > ( ) ;
27+ return doNotAutoRegisterAttribute != null ;
28+ }
29+
30+ /// <summary>
31+ /// Check if class marked by multiple lifetime attributes
32+ /// </summary>
33+ /// <param name="type">type</param>
34+ /// <returns></returns>
35+ public static bool IsMultipleLifetime ( this Type type )
36+ {
37+ var attributes = type . GetCustomAttributes ( true )
38+ . Select ( x => x . GetType ( ) )
39+ . ToList ( ) ;
40+ if ( attributes . Any ( ) )
41+ {
42+ return new List < bool >
43+ {
44+ attributes . Contains ( typeof ( RegisterAsTransientAttribute ) ) ,
45+ attributes . Contains ( typeof ( RegisterAsScopedAttribute ) ) ,
46+ attributes . Contains ( typeof ( RegisterAsSingletonAttribute ) )
47+ } . Count ( x => x ) > 1 ;
48+ }
49+
50+ return false ;
51+ }
52+
53+ /// <summary>
54+ /// Returns service lifetime
55+ /// </summary>
56+ /// <param name="type">type</param>
57+ /// <returns></returns>
58+ public static ServiceLifetime GetTypeLiteTime ( this Type type )
59+ {
60+ if ( type . GetCustomAttribute < RegisterAsTransientAttribute > ( ) != null )
61+ {
62+ return ServiceLifetime . Transient ;
63+ }
64+
65+ if ( type . GetCustomAttribute < RegisterAsScopedAttribute > ( ) != null )
66+ {
67+ return ServiceLifetime . Scoped ;
68+ }
69+
70+ if ( type . GetCustomAttribute < RegisterAsSingletonAttribute > ( ) != null )
71+ {
72+ return ServiceLifetime . Singleton ;
73+ }
74+
75+ return ServiceLifetime . Transient ;
76+ }
77+ }
78+ }
0 commit comments