views:

132

answers:

2

I'm having the following problem when using reflection.

The following statement evaluates to false:

object[] attributes = someType.GetCustomAttributes(true);

if (attributes[0] is NUnit.Framework.TestFixtureAttribute)
    return true;

However this evaluates to true:

object[] attributes = someType.GetCustomAttributes(true);

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

Any ideas why?

+8  A: 

Perhaps it's loading a different version of the assembly?

Compare attributes[0].GetType().Assembly with typeof(NUnit.Framework.TestFixtureAttribute).Assembly.

Just perform a reference type comparison - even if two Assembly instances have been loaded from the exact same file, if they're two separate instances, any types created from them will be distinct (making is fail).

Jon Skeet
I concur wholeheartedly.
Will
typeof(NUnit.Framework.TestFixtureAttribute).Assembly yieldsThe type 'NUnit.Framework.TestFixtureAttribute' exists in both 'nunit.framework.dll' and 'nunit.framework.dll'So I presume there are 2 versions of 'nunit.framework.dll' on my system. Thanks!
jamesaharvey
+3  A: 

The class you're testing has probably been built with a different version of nunit.framework.dll

Philippe Leybaert