@@ -22,12 +22,12 @@ public class SupportedOnAttribute : Attribute
22
22
/// </summary>
23
23
/// <param name="subject">Which type will support <paramref name="target" />.</param>
24
24
/// <param name="target">Defines what to support.</param>
25
- public SupportedOnAttribute ( Type subject , Type target )
25
+ public SupportedOnAttribute ( Type target )
26
26
{
27
- // Call directly registration here. This avoids storing properties in the memory footprint
28
- IsSupportedOn . RegisterStaticRelation ( subject , target ) ;
29
- IsSupportedOn . RegisterDynamicRelation ( subject , target ) ;
27
+ this . target = target ;
30
28
}
29
+
30
+ public Type target { get ; }
31
31
}
32
32
33
33
/// <summary>
@@ -76,12 +76,14 @@ internal static void RegisterStaticRelation(Type subject, Type target)
76
76
// So there must be no situation where the properties are not found.
77
77
78
78
var hasIsSupportedOn = typeof ( HasIsSupportedOn < > ) . MakeGenericType ( subject ) ;
79
+
79
80
// ReSharper disable once PossibleNullReferenceException
80
81
hasIsSupportedOn
81
82
. GetProperty ( nameof ( HasIsSupportedOn < bool > . Value ) , BindingFlags . Static | BindingFlags . Public )
82
83
. SetValue ( null , true ) ;
83
84
84
85
var isSupportedOn = typeof ( IsSupportedOn < , > ) . MakeGenericType ( subject , target ) ;
86
+
85
87
// ReSharper disable once PossibleNullReferenceException
86
88
isSupportedOn
87
89
. GetProperty ( nameof ( IsSupportedOn < bool , bool > . internalValue ) , BindingFlags . Static | BindingFlags . NonPublic )
@@ -139,6 +141,7 @@ public static bool HasRelations(Type subject)
139
141
/// Use <see cref="Value" /> to know if <typeparamref name="TSubject" /> explicitly support at least one type.
140
142
/// </summary>
141
143
/// <typeparam name="TSubject">The type to query.</typeparam>
144
+
142
145
// ReSharper disable once UnusedTypeParameter
143
146
public struct HasIsSupportedOn < TSubject >
144
147
{
@@ -147,8 +150,9 @@ public struct HasIsSupportedOn<TSubject>
147
150
/// <typeparam name="TSubject" />
148
151
/// explicitly support at least one type.
149
152
/// </summary>
153
+
150
154
// ReSharper disable once StaticMemberInGenericType
151
- public static bool Value { get ; } = false ;
155
+ public static bool Value { get ; set ; } = false ;
152
156
}
153
157
154
158
/// <summary>
@@ -157,11 +161,12 @@ public struct HasIsSupportedOn<TSubject>
157
161
/// </summary>
158
162
/// <typeparam name="TSubject">The subject to query.</typeparam>
159
163
/// <typeparam name="TTarget">The type that defines what to support.</typeparam>
164
+
160
165
// ReSharper disable once UnusedTypeParameter
161
166
public struct IsSupportedOn < TSubject , TTarget >
162
167
{
163
168
// ReSharper disable once StaticMemberInGenericType
164
- internal static bool internalValue { get ; } = false ;
169
+ internal static bool internalValue { get ; set ; } = false ;
165
170
166
171
/// <summary>
167
172
/// Use it to know if <typeparamref name="TSubject" /> explicitly supports <typeparamref name="TTarget" />.
@@ -191,8 +196,12 @@ static void Initialize()
191
196
// Note: Querying type with attribute with TypeCache is 4x faster that querying for assembly attribute
192
197
foreach ( var type in TypeCache . GetTypesWithAttribute < SupportedOnAttribute > ( ) )
193
198
{
194
- // Trigger attribute constructor here
195
- var _ = type . GetCustomAttributes ( typeof ( SupportedOnAttribute ) ) . FirstOrDefault ( ) as SupportedOnAttribute ;
199
+ var attribute = type . GetCustomAttributes ( typeof ( SupportedOnAttribute ) ) . FirstOrDefault ( ) as SupportedOnAttribute ;
200
+ if ( attribute ? . target == null )
201
+ continue ;
202
+
203
+ IsSupportedOn . RegisterStaticRelation ( type , attribute . target ) ;
204
+ IsSupportedOn . RegisterDynamicRelation ( type , attribute . target ) ;
196
205
}
197
206
}
198
207
}
0 commit comments