Skip to content

Commit 4afd2d5

Browse files
committed
feat: Allowed adding more than one base type for the Inherits attribute
It may come in handy when types implement multiple interfaces
1 parent 3f28224 commit 4afd2d5

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
namespace TypeReferences
22
{
33
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
46
using SolidUtilities.Extensions;
57

68
/// <summary>
@@ -10,20 +12,18 @@
1012
[AttributeUsage(AttributeTargets.Field)]
1113
public class InheritsAttribute : TypeOptionsAttribute
1214
{
15+
private readonly IEnumerable<Type> _baseTypes;
16+
1317
/// <summary>
1418
/// Initializes a new instance of the <see cref="InheritsAttribute"/> class.
1519
/// </summary>
1620
/// <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)
1823
{
19-
BaseType = baseType;
24+
_baseTypes = additionalBaseTypes.Append(baseType);
2025
}
2126

22-
/// <summary>
23-
/// Gets the type that selectable types must derive from.
24-
/// </summary>
25-
public Type BaseType { get; private set; }
26-
2727
/// <summary>
2828
/// Allows to choose the base type from the drop-down as well.
2929
/// Defaults to a value of <c>false</c> unless explicitly specified.
@@ -34,25 +34,25 @@ public InheritsAttribute(Type baseType)
3434
/// Allows abstract classes and interfaces to be selected from drop-down.
3535
/// Defaults to a value of <c>false</c> unless explicitly specified.
3636
/// </summary>
37-
public bool AllowAbstract { get; set; } = false;
37+
public bool AllowAbstract { get; set; }
3838

3939
/// <inheritdoc/>
4040
public override bool MatchesRequirements(Type type)
4141
{
42-
if (type == BaseType && !IncludeBaseType)
42+
if (_baseTypes.Contains(type) && !IncludeBaseType)
4343
{
4444
return false;
4545
}
4646

4747
// Include base type in the drop-down even if it is abstract.
4848
// If the user set IncludeBaseType to true, they probably want to include the base type in the dropdown
4949
// even though it is abstract.
50-
if (type == BaseType)
50+
if (_baseTypes.Contains(type))
5151
return true;
5252

5353
bool passesAbstractConstraint = AllowAbstract || !type.IsAbstract;
5454

55-
return type.InheritsFrom(BaseType) && passesAbstractConstraint && base.MatchesRequirements(type);
55+
return _baseTypes.All(type.InheritsFrom) && passesAbstractConstraint && base.MatchesRequirements(type);
5656
}
5757
}
5858
}

0 commit comments

Comments
 (0)