Skip to content

Commit

Permalink
Added a helper to retrieve all controllers and methods from an assembly
Browse files Browse the repository at this point in the history
(%release-note:

- Added a helper class to retrieve all controllers and methods from an assembly

%)
  • Loading branch information
Farshad DASHTI authored and Farshad DASHTI committed Oct 25, 2024
1 parent 7a3b9a9 commit f11286f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/DfE.CoreLibs.Utilities/DfE.CoreLibs.Utilities.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
</ItemGroup>

</Project>
28 changes: 28 additions & 0 deletions src/DfE.CoreLibs.Utilities/Helpers/ControllerHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Microsoft.AspNetCore.Mvc;
using System.Reflection;

namespace DfE.CoreLibs.Utilities.Helpers
{
/// <summary>
/// Retrieves all the controllers and their methods from the provided assembly.
/// </summary>
public static class ControllerHelper
{
public static IEnumerable<(Type Controller, MethodInfo Method)> GetAllControllerMethodsTuples(Assembly assembly)
{
var controllers = assembly.GetTypes()
.Where(type => typeof(ControllerBase).IsAssignableFrom(type));

foreach (var controller in controllers)
{
var methods = controller.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly)
.Where(m => m.IsPublic && !m.IsDefined(typeof(NonActionAttribute)));

foreach (var method in methods)
{
yield return (controller, method);
}
}
}
}
}

0 comments on commit f11286f

Please sign in to comment.