2009-09-11 71 views
4

使用反射时出现以下问题。C#反射问题

下面的语句评估为假:

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; 

任何想法,为什么?

回答

8

也许它正在加载不同版本的程序集?

比较attributes[0].GetType().Assemblytypeof(NUnit.Framework.TestFixtureAttribute).Assembly

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

+0

我同意全心全意。 – Will 2009-09-11 14:15:01

+0

typeof运算(NUnit.Framework.TestFixtureAttribute).Assembly产生 'NUnit.Framework.TestFixtureAttribute' 存在于两个 'nunit.framework.dll中' 和 'nunit.framework.dll中' 所以我假定的类型有2个版本'nunit.framework.dll'在我的系统上。谢谢! – jamesaharvey 2009-09-11 14:38:59

3

你正在测试的类可能已经建有一个不同的版本nunit.framework.dll中