我不确定为什么下面的方法总是返回 false
// method to check for presence of TestCaseAttribute
private static bool hasTestCaseAttribute(MemberInfo m)
{
foreach (object att in m.GetCustomAttributes(true))
{
Console.WriteLine(att.ToString());
if (att is TestCase.TestCaseAttribute) // also tried if (att is TestCaseAttribute)
{
return true;
}
}
return false;
}
即使控制台输出看起来像这样:
TestCase.DateAttribute
TestCase.AuthorAttribute
TestCase.TestCaseAttribute
我在这里错过了什么?
编辑;这种方法似乎有效...
private static bool hasTestCaseAttribute(MemberInfo m)
{
if (m.GetCustomAttributes(typeof(TestCaseAttribute), true).Any())
{
return true;
}
else
{
return false;
}
}
最佳答案
这应该可以解决问题。
private static bool hasTestCaseAttribute(MemberInfo m)
{
return m.GetCustomAttributes(typeof(TestCaseAttribute), true).Any();
}
关于c# - 检查自定义属性的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14554447/