Skip to content

Commit c2aa734

Browse files
Stanley Gehmankhellang
authored andcommitted
Eliminate new API methods for opting in to compiler-generated types
- We can simplify this to re-using existing methods in the API.
1 parent 26049af commit c2aa734

File tree

4 files changed

+7
-37
lines changed

4 files changed

+7
-37
lines changed

src/Scrutor/IImplementationTypeFilter.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,6 @@ public interface IImplementationTypeFilter : IFluentInterface
3131
/// <param name="types">The types that should be assignable from the matching types.</param>
3232
/// <exception cref="ArgumentNullException">If the <paramref name="types"/> argument is <c>null</c>.</exception>
3333
IImplementationTypeFilter AssignableToAny(IEnumerable<Type> types);
34-
35-
/// <summary>
36-
/// Will include all compiler-generated types that derive from any of the specified <paramref name="types"/>.
37-
/// This can be useful to include statically-compiled views with some UI frameworks.
38-
/// </summary>
39-
/// <param name="types">The base types for which to allow compiler-generated subclasses</param>
40-
/// <returns></returns>
41-
IImplementationTypeFilter IncludeCompilerGeneratedSubclassesOf(params Type[] types);
42-
43-
/// <summary>
44-
/// Will include all compiler-generated types that derive from any of the specified <paramref name="types"/>.
45-
/// This can be useful to include statically-compiled views with some UI frameworks.
46-
/// </summary>
47-
/// <param name="types">The base types for which to allow compiler-generated subclasses</param>
48-
/// <returns></returns>
49-
IImplementationTypeFilter IncludeCompilerGeneratedSubclassesOf(IEnumerable<Type> types);
5034

5135
/// <summary>
5236
/// Will match all types that has an attribute of type <typeparamref name="T"/> defined.

src/Scrutor/ImplementationTypeFilter.cs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public ImplementationTypeFilter(ISet<Type> types)
1414

1515
internal ISet<Type> Types { get; private set; }
1616

17-
internal ISet<Type> CompilerGeneratedBases { get; private set; } = new HashSet<Type>();
17+
internal bool IncludeCompilerGeneratedTypes { get; private set; }
1818

1919
public IImplementationTypeFilter AssignableTo<T>()
2020
{
@@ -42,22 +42,6 @@ public IImplementationTypeFilter AssignableToAny(IEnumerable<Type> types)
4242
return Where(t => types.Any(t.IsBasedOn));
4343
}
4444

45-
public IImplementationTypeFilter IncludeCompilerGeneratedSubclassesOf(params Type[] types)
46-
{
47-
Preconditions.NotNull(types, nameof(types));
48-
49-
return IncludeCompilerGeneratedSubclassesOf(types.AsEnumerable());
50-
}
51-
52-
public IImplementationTypeFilter IncludeCompilerGeneratedSubclassesOf(IEnumerable<Type> types)
53-
{
54-
Preconditions.NotNull(types, nameof(types));
55-
56-
var typesList = types.ToList();
57-
typesList.ForEach(t => CompilerGeneratedBases.Add(t));
58-
return Where(t => !t.HasAttribute<CompilerGeneratedAttribute>() || typesList.Any(t.IsBasedOn));
59-
}
60-
6145
public IImplementationTypeFilter WithAttribute<T>() where T : Attribute
6246
{
6347
return WithAttribute(typeof(T));
@@ -67,6 +51,7 @@ public IImplementationTypeFilter WithAttribute(Type attributeType)
6751
{
6852
Preconditions.NotNull(attributeType, nameof(attributeType));
6953

54+
IncludeCompilerGeneratedTypes |= attributeType == typeof(CompilerGeneratedAttribute);
7055
return Where(t => t.HasAttribute(attributeType));
7156
}
7257

src/Scrutor/ImplementationTypeSelector.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Diagnostics.CodeAnalysis;
44
using System.Linq;
55
using System.Reflection;
6+
using System.Runtime.CompilerServices;
67
using Microsoft.Extensions.DependencyInjection;
78
using Microsoft.Extensions.DependencyModel;
89

@@ -49,8 +50,8 @@ public IServiceTypeSelector AddClasses(Action<IImplementationTypeFilter> action,
4950

5051
action(filter);
5152

52-
if (!filter.CompilerGeneratedBases.Any())
53-
filter.IncludeCompilerGeneratedSubclassesOf(Enumerable.Empty<Type>());
53+
if (!filter.IncludeCompilerGeneratedTypes)
54+
filter.WithoutAttribute<CompilerGeneratedAttribute>();
5455

5556
return AddSelector(filter.Types);
5657
}

test/Scrutor.Tests/ScanningTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,15 +571,15 @@ public void AsSelfWithInterfacesHandlesOpenGenericTypes()
571571
}
572572

573573
[Fact]
574-
public void ScanShouldIncludeSpecifiedCompilerGeneratedClasses()
574+
public void ShouldAllowOptInToCompilerGeneratedTypes()
575575
{
576576
var provider = ConfigureProvider(services =>
577577
{
578578
services.Scan(scan => scan
579579
.FromAssemblyOf<AllowedCompilerGeneratedSubclass>()
580580
.AddClasses(classes => classes
581+
.WithAttribute<CompilerGeneratedAttribute>()
581582
.AssignableTo<AllowedCompilerGeneratedBase>()
582-
.IncludeCompilerGeneratedSubclassesOf(typeof(AllowedCompilerGeneratedBase))
583583
)
584584
.AsSelf()
585585
.WithTransientLifetime());

0 commit comments

Comments
 (0)