1
1
namespace TypeReferences
2
2
{
3
3
using System ;
4
+ using System . Collections . Generic ;
5
+ using System . Linq ;
4
6
using SolidUtilities . Extensions ;
5
7
6
8
/// <summary>
10
12
[ AttributeUsage ( AttributeTargets . Field ) ]
11
13
public class InheritsAttribute : TypeOptionsAttribute
12
14
{
15
+ private readonly IEnumerable < Type > _baseTypes ;
16
+
13
17
/// <summary>
14
18
/// Initializes a new instance of the <see cref="InheritsAttribute"/> class.
15
19
/// </summary>
16
20
/// <param name="baseType">Type that selectable types must inherit from.</param>
17
- public InheritsAttribute ( Type baseType )
21
+ /// <param name="additionalBaseTypes">Additional types the selectable types must inherit from (e.g. multiple interfaces).</param>
22
+ public InheritsAttribute ( Type baseType , params Type [ ] additionalBaseTypes )
18
23
{
19
- BaseType = baseType ;
24
+ _baseTypes = additionalBaseTypes . Append ( baseType ) ;
20
25
}
21
26
22
- /// <summary>
23
- /// Gets the type that selectable types must derive from.
24
- /// </summary>
25
- public Type BaseType { get ; private set ; }
26
-
27
27
/// <summary>
28
28
/// Allows to choose the base type from the drop-down as well.
29
29
/// Defaults to a value of <c>false</c> unless explicitly specified.
@@ -34,25 +34,25 @@ public InheritsAttribute(Type baseType)
34
34
/// Allows abstract classes and interfaces to be selected from drop-down.
35
35
/// Defaults to a value of <c>false</c> unless explicitly specified.
36
36
/// </summary>
37
- public bool AllowAbstract { get ; set ; } = false ;
37
+ public bool AllowAbstract { get ; set ; }
38
38
39
39
/// <inheritdoc/>
40
40
public override bool MatchesRequirements ( Type type )
41
41
{
42
- if ( type == BaseType && ! IncludeBaseType )
42
+ if ( _baseTypes . Contains ( type ) && ! IncludeBaseType )
43
43
{
44
44
return false ;
45
45
}
46
46
47
47
// Include base type in the drop-down even if it is abstract.
48
48
// If the user set IncludeBaseType to true, they probably want to include the base type in the dropdown
49
49
// even though it is abstract.
50
- if ( type == BaseType )
50
+ if ( _baseTypes . Contains ( type ) )
51
51
return true ;
52
52
53
53
bool passesAbstractConstraint = AllowAbstract || ! type . IsAbstract ;
54
54
55
- return type . InheritsFrom ( BaseType ) && passesAbstractConstraint && base . MatchesRequirements ( type ) ;
55
+ return _baseTypes . All ( type . InheritsFrom ) && passesAbstractConstraint && base . MatchesRequirements ( type ) ;
56
56
}
57
57
}
58
58
}
0 commit comments