我有一个.NET Core3WebApi应用程序,在启动过程中,我需要访问我的所有控制器方法来查看它们拥有哪些属性,并将这些信息保存到字典中。
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers(); // is is possible to get more info about the controllers here?
}我知道我可以使用反射来实现这个目标,在二进制文件中使用Controller搜索所有类,循环遍历它们的方法并访问它们的属性。我的问题是:还有别的办法吗?既然我需要在启动中添加控制器,也许我可以获得更多关于它们的信息?
发布于 2020-01-08 17:01:20
您可以使用简单的喷射器做的在容器中注册控制器的方式。类似于:
services.AddControllers();
var manager = (ApplicationPartManager)services
.LastOrDefault(d => d.ServiceType == typeof(ApplicationPartManager))
.ImplementationInstance;
var feature = new ControllerFeature();
manager.PopulateFeature(feature);
var controllerTypes = feature.Controllers.Select(t => t.AsType());https://stackoverflow.com/questions/59650360
复制相似问题