代码之家  ›  专栏  ›  技术社区  ›  jamesaharvey

C反射问题

  •  4
  • jamesaharvey  · 技术社区  · 16 年前

    我在使用反射时遇到以下问题。

    以下语句的计算结果为false:

    object[] attributes = someType.GetCustomAttributes(true);
    
    if (attributes[0] is NUnit.Framework.TestFixtureAttribute)
        return true;
    

    然而,这一结果为真:

    object[] attributes = someType.GetCustomAttributes(true);
    
    if (attributes[0].ToString() == "NUnit.Framework.TestFixtureAttribute")
        return true;
    

    有什么想法吗?

    2 回复  |  直到 16 年前
        1
  •  8
  •   Jon Skeet    16 年前

    也许它正在加载程序集的另一个版本?

    比较 attributes[0].GetType().Assembly 具有 typeof(NUnit.Framework.TestFixtureAttribute).Assembly .

    只需执行引用类型比较-即使是两个 Assembly 实例已从完全相同的文件加载,如果它们是两个单独的实例,则从它们创建的任何类型都将是不同的(使 is 失败)。

        2
  •  3
  •   Philippe Leybaert    16 年前

    您正在测试的类可能是用不同版本的nunit.framework.dll生成的。