Skip to content

Commit 215b04e

Browse files
Remove use of Reflection; not AOT friendly
1 parent 16b31f2 commit 215b04e

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/AspNetCore/WebApi/src/Asp.Versioning.Mvc/ApplicationModels/ApiBehaviorSpecification.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Asp.Versioning.ApplicationModels;
44

55
using Microsoft.AspNetCore.Mvc.ApplicationModels;
6+
using Microsoft.AspNetCore.Mvc.Infrastructure;
67
using System.Reflection;
78

89
/// <summary>
@@ -11,17 +12,22 @@ namespace Asp.Versioning.ApplicationModels;
1112
[CLSCompliant( false )]
1213
public sealed class ApiBehaviorSpecification : IApiControllerSpecification
1314
{
14-
static ApiBehaviorSpecification()
15+
/// <inheritdoc />
16+
public bool IsSatisfiedBy( ControllerModel controller )
1517
{
16-
const string ApiBehaviorApplicationModelProviderTypeName = "Microsoft.AspNetCore.Mvc.ApplicationModels.ApiBehaviorApplicationModelProvider, Microsoft.AspNetCore.Mvc.Core";
17-
var type = Type.GetType( ApiBehaviorApplicationModelProviderTypeName, throwOnError: true )!;
18-
var method = type.GetRuntimeMethods().Single( m => m.Name == "IsApiController" );
18+
if ( controller == null )
19+
{
20+
throw new ArgumentNullException( nameof( controller ) );
21+
}
1922

20-
IsApiController = (Func<ControllerModel, bool>) method.CreateDelegate( typeof( Func<ControllerModel, bool> ) );
21-
}
23+
// REF: https://github.com/dotnet/aspnetcore/blob/main/src/Mvc/Mvc.Core/src/ApplicationModels/ApiBehaviorApplicationModelProvider.cs
24+
if ( controller.Attributes.OfType<IApiBehaviorMetadata>().Any() )
25+
{
26+
return true;
27+
}
2228

23-
private static Func<ControllerModel, bool> IsApiController { get; }
29+
var assembly = controller.ControllerType.Assembly;
2430

25-
/// <inheritdoc />
26-
public bool IsSatisfiedBy( ControllerModel controller ) => IsApiController( controller );
31+
return assembly.GetCustomAttributes().OfType<IApiBehaviorMetadata>().Any();
32+
}
2733
}

0 commit comments

Comments
 (0)