1
- using BidirectionalDict ;
1
+ using BidirectionalDict ;
2
2
using System ;
3
3
using System . Collections . Generic ;
4
4
using System . Collections . ObjectModel ;
@@ -31,33 +31,42 @@ public CacheInitializer InitWith(params Assembly[] assemblies)
31
31
32
32
foreach ( var enumType in enumTypes )
33
33
{
34
- var fields = enumType . GetFields ( ) ;
34
+ InitWith ( enumType ) ;
35
+ }
36
+ }
35
37
36
- var temptDict = new BiDictionary < int , string > ( ) ;
38
+ return this ;
39
+ }
37
40
38
- foreach ( var field in fields . Where ( x => x . IsStatic ) )
39
- {
40
- var attr = field . GetCustomAttribute < StringRepresentationAttribute > ( ) ;
41
+ /// <summary>
42
+ /// Initializes the cache with a given enum.
43
+ /// </summary>
44
+ /// <typeparam name="TEnum">The enum which should be added to the cache.</typeparam>
45
+ public CacheInitializer InitWith < TEnum > ( ) where TEnum : struct , Enum
46
+ {
47
+ InitWith ( typeof ( TEnum ) ) ;
41
48
42
- if ( attr is { } )
43
- {
44
- temptDict . TryAdd ( ( int ) field . GetValue ( enumType ) ! , attr . StringRepresentation ) ;
45
- }
46
- }
49
+ return this ;
50
+ }
47
51
48
- if ( temptDict . Count > 0 )
49
- {
50
- _tempCache . Add ( enumType , temptDict ) ;
51
- }
52
- }
52
+ /// <summary>
53
+ /// Initializes the cache with a set of given enums.
54
+ /// </summary>
55
+ /// <param name="enums">The enums which should be added to the cache.</param>
56
+ public CacheInitializer InitWith ( params Enum [ ] enums )
57
+ {
58
+ foreach ( var enumVal in enums )
59
+ {
60
+ InitWith ( enumVal ) ;
53
61
}
54
62
55
63
return this ;
56
64
}
57
65
58
- public CacheInitializer InitWith < TEnum > ( ) where TEnum : struct , Enum
66
+ private void InitWith ( Type enumType )
59
67
{
60
- var enumType = typeof ( TEnum ) ;
68
+ if ( ! enumType . IsEnum )
69
+ throw new ArgumentException ( "The type provided is not of type enum" , nameof ( enumType ) ) ;
61
70
62
71
var fields = enumType . GetFields ( ) ;
63
72
@@ -77,8 +86,6 @@ public CacheInitializer InitWith<TEnum>() where TEnum : struct, Enum
77
86
{
78
87
_tempCache . Add ( enumType , temptDict ) ;
79
88
}
80
-
81
- return this ;
82
89
}
83
90
84
91
internal IReadOnlyDictionary < Type , IReadOnlyBiDictionary < int , string > > CustructCache ( )
0 commit comments