反射 - 通过名称访问自定义属性

3
我正在尝试查看一个给定方法是否带有属性(该属性为 NUnit.Framework.TestAttribute),但我需要能够检查该属性,无论属性的版本如何。目前,我在项目中使用反射的是 nunit.framework.dll 版本 2.6.2,而在测试中使用的是 dll 的版本 2.6.0。反射未找到该属性。
有没有一种方法可以实现这个功能?
bool isTest = method.GetCustomAttributes(typeof(TestAttribute), true).Length > 0;

没有访问正确版本的TestAttribute dll,怎么办?

其中,方法的类型为MethodInfo

1个回答

4

您可以获取所有属性并按名称过滤:

method.GetCustomAttributes(true)
      .Where(a => a.GetType().FullName == "NUnit.Framework.TestAttribute");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接