Currently, I'm trying to identify which "Controller" classes in my assembly have the [Authorize] attribute associated with them using Reflection and LINQ.
const bool allInherited = true;
var myAssembly = System.Reflection.Assembly.GetExecutingAssembly();
var controllerList = from type in myAssembly.GetTypes()
where type.Name.Contains("Controller")
where !type.IsAbstract
let attribs = type.GetCustomAttributes(allInherited)
where attribs.Contains("Authorize")
select type;
controllerList.ToList();
This code almost works.
If I trace through the LINQ statement step-by-step, I can see that when I "mouseover" the "attribs" range variable that I define in the LINQ statement is populated with a single Attribute and that attribute happens to be of type AuthorizeAttribute. It looks sort of like this:
[-] attribs | {object[1]}
[+] [0] | {System.Web.Mvc.AuthorizeAttribute}
Clearly, this line in my LINQ statement is wrong:
where attribs.Contains("Authorize")
What should I write there instead to detect if "attribs" contains the AuthorizeAttribute type or not?
I think that a better way of accomplishing this is:
Or if you're looking for any attribute that has "Authorize" in it:
you would want to do
you were comparing an object with a string so the check was always failing, this should work.