我正在尝试查看给定方法是否用属性修饰(所讨论的属性是 NUnit.Framework.TestAttribute
),但无论如何我都需要能够检查该属性属性是什么版本。目前,我在使用反射的项目中有 nunit.framework.dll
2.6.2 版,在测试中有 2.6.0 版的 dll。反射没有找到属性。
有什么办法吗
bool isTest = method.GetCustomAttributes(typeof(TestAttribute), true).Length > 0;
无法访问正确版本的 TestAttribute
dll?
其中方法的类型为 MethodInfo
。
最佳答案
您可以获取所有属性并按名称过滤:
method.GetCustomAttributes(true)
.Where(a => a.GetType().FullName == "NUnit.Framework.TestAttribute");
关于c# - 反射 - 按名称访问自定义属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18197452/