Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 725
jamesaharvey Avatar asked Dec 23 '22 09:12

jamesaharvey


1 Answers

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

like image 147
Jon Skeet Avatar answered Jan 06 '23 05:01

Jon Skeet