I'm trying to get all the attributes of a certain type present in the assembly. In my specific case I have attributes on Controller and others attributes on Actions (MVC). With this code I can get what I want, but I'm quite sure there is a way to avoid the union
var assemblyTypes = Assembly.GetExecutingAssembly().GetTypes();
var myAttributes = assemblyTypes
.SelectMany(x => x.GetCustomAttributes<MyAttribute>()).ToList();
myAttributes = myAttributes.Union(assemblyTypes
.SelectMany(x => x.GetMethods())
.SelectMany(x => x.GetCustomAttributes<MyAttribute>())).ToList();
myAttributes = myAttributes.Distinct().ToList();
Aucun commentaire:
Enregistrer un commentaire