是否有更快/更好的方法来获取在C#中应用属性的方法

jww*_*art 5 c# reflection asp.net-mvc appdomain

我有一个这样的标记界面:

[AttributeUsage(AttributeTargets.Method, AllowMultiple=false, Inherited=true)]
public class MyAttribute : Attribute
{
}
Run Code Online (Sandbox Code Playgroud)

我想将它应用于不同程序集中不同类的方法...

然后我想为所有应用了此属性的方法获取MethodInfo.我需要搜索整个AppDomain并获取所有这些方法的引用.

我知道我们可以获得所有类型然后获得所有方法,但有更快/更好的方法来做到这一点吗?......或者这是获取我需要的信息的最快方式吗?

(我正在使用ASP.NET MVC 1.0,C#,./ NET 3.5)

谢谢堆!

Mar*_*ell 14

最终,不 - 你必须扫描它们.LINQ让它相当无痛.

        var qry = from asm in AppDomain.CurrentDomain.GetAssemblies()
                  from type in asm.GetTypes()
                  from method in type.GetMethods()
                  where Attribute.IsDefined(method, typeof(MyAttribute))
                  select method;
Run Code Online (Sandbox Code Playgroud)

请注意,这仅按"原样" 扫描加载的程序集.