jamesaharvey
jamesaharvey

Reputation: 14291

C# Reflection Problem

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?

Upvotes: 4

Views: 531

Answers (2)

Philippe Leybaert
Philippe Leybaert

Reputation: 171774

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

Upvotes: 3

Jon Skeet
Jon Skeet

Reputation: 1500525

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).

Upvotes: 8

Related Questions