我編寫(xiě)此代碼是為了在我的項(xiàng)目中查找控制器列表。 public IEnumerable<Type> GetControllerDescovery(Assembly assemblies) { var controllers = assemblies.GetExportedTypes() .Where(t => typeof(ControllerBase) .IsAssignableFrom(t)) .Select(t => t). ToList(); return controllers; }它向我展示了這個(gè)輸出:"IranNews.Controllers.V1.CategoryController, IranNews, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null","IranNews.Controllers.V1.RoleManagerController, IranNews, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null","IranNews.Controllers.V1.UserController, IranNews, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null","IranNews.Controllers.V1.ValuesController, IranNews, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"但我需要從[Display(Name = "UserManager")]給我看這個(gè):UserManager我怎樣才能做到這一點(diǎn) ?????
1 回答

MMMHUHU
TA貢獻(xiàn)1834條經(jīng)驗(yàn) 獲得超8個(gè)贊
嘗試這個(gè):
typeof(ControllerBase) .GetProperties() .Select(x => x.GetCustomAttribute<DisplayAttribute>()) .Where(x => x != null) .Select(x => x.Name) .ToList();
更新: 這是一個(gè)工作示例:
Assembly.GetExecutingAssembly().GetExportedTypes() .Where(t => typeof(ControllerBase).IsAssignableFrom(t)) .Select(t => t.GetCustomAttribute<DisplayAttribute>()) .Where(x => x != null) .Select(x => x.Name) .ToList();
- 1 回答
- 0 關(guān)注
- 117 瀏覽
添加回答
舉報(bào)
0/150
提交
取消