gpt4 book ai didi

c# - 检查自定义属性的方法

转载 作者:太空宇宙 更新时间:2023-11-03 19:18:07 24 4
gpt4 key购买 nike

我不确定为什么下面的方法总是返回 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/

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