-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a helper to retrieve all controllers and methods from an assembly
(%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
Showing
2 changed files
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} | ||
} |