gpt4 book ai didi

c# - 有没有更快/更好的方法来获取在 C# 中应用属性的方法

转载 作者:太空狗 更新时间:2023-10-29 22:08:47 25 4
gpt4 key购买 nike

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

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

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

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

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

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

非常感谢!

最佳答案

最终,不 - 你必须扫描它们。不过,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;

请注意,这只会“按原样”扫描加载的程序集。

关于c# - 有没有更快/更好的方法来获取在 C# 中应用属性的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2218689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com