C#反思问题

jam*_*vey 4 .net c# reflection

使用反射时遇到以下问题.

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

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

if (attributes[0] is NUnit.Framework.TestFixtureAttribute)
    return true;
Run Code Online (Sandbox Code Playgroud)

但是这个评估为真:

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

if (attributes[0].ToString() == "NUnit.Framework.TestFixtureAttribute")
    return true;
Run Code Online (Sandbox Code Playgroud)

有什么想法吗?

Jon*_*eet 8

也许它正在加载不同版本的组件?

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

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