使用 LINQ 和反射:如何查询程序集中具有 [Authorize] 属性的所有类?

发布于 2024-09-04 23:13:34 字数 934 浏览 4 评论 0原文

目前,我正在尝试使用反射和 LINQ 来识别程序集中的哪些“控制器”类具有与其关联的 [Authorize] 属性。

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();

这段代码几乎可以工作。

如果我逐步跟踪 LINQ 语句,我可以看到,当我将鼠标悬停在 LINQ 语句中定义的“attribs”范围变量时,会使用单个属性填充该变量,并且该属性恰好是 AuthorizeAttribute 类型。它看起来有点像这样:

[-] attribs | {object[1]}
   [+]  [0] | {System.Web.Mvc.AuthorizeAttribute}

显然,我的 LINQ 语句中的这一行是错误的:

where attribs.Contains("Authorize")

我应该在那里写什么来检测“attribs”是否包含 AuthorizeAttribute 类型?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鱼窥荷 2024-09-11 23:13:34

您想要

attribs.Any(a => a.GetType().Equals(typeof(AuthorizeAttribute))

将对象与字符串进行比较,因此检查总是失败,这应该可行。

you would want to do

attribs.Any(a => a.GetType().Equals(typeof(AuthorizeAttribute))

you were comparing an object with a string so the check was always failing, this should work.

天涯沦落人 2024-09-11 23:13:34

我认为实现此目的的更好方法是:

var controllerList = (from type in Assembly.GetExecutingAssembly().GetTypes()
                      where !type.IsAbstract
                      where type.IsSubclassOf(typeof(Controller)) || type.IsSubclassOf(typeof(System.Web.Http.ApiController))
                      where type.IsDefined(typeof(AuthorizeAttribute), allInherited)
                      select type).ToList();

或者如果您正在寻找其中包含“授权”的任何属性:

var controllerList = from type in myAssembly.GetTypes()
                     where type.Name.Contains("Controller")
                     where !type.IsAbstract
                     let attrs = type.GetCustomAttributes(allInherited).OfType<Attribute>()
                     where attrs.Any(a => a.Name.Contains("Authorize"))
                     select type;

I think that a better way of accomplishing this is:

var controllerList = (from type in Assembly.GetExecutingAssembly().GetTypes()
                      where !type.IsAbstract
                      where type.IsSubclassOf(typeof(Controller)) || type.IsSubclassOf(typeof(System.Web.Http.ApiController))
                      where type.IsDefined(typeof(AuthorizeAttribute), allInherited)
                      select type).ToList();

Or if you're looking for any attribute that has "Authorize" in it:

var controllerList = from type in myAssembly.GetTypes()
                     where type.Name.Contains("Controller")
                     where !type.IsAbstract
                     let attrs = type.GetCustomAttributes(allInherited).OfType<Attribute>()
                     where attrs.Any(a => a.Name.Contains("Authorize"))
                     select type;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文