【发布时间】:2015-01-09 05:43:00
【问题描述】:
下面的 F# 脚本因 FS0041 失败(无法根据此程序点之前的类型信息确定方法“GetCustomAttributes”的唯一重载。可能需要类型注释)。你是怎么解决的?
如果我添加类型注释,例如
let getattr (el : Sometype) = Attribute.CustomAttributes(el, true)
它因 FS0001 失败(“Sometype”类型与“Assembly”类型不兼容。 可能的过载:...省略长行...)
open System
open System.Reflection
let nunit_tattr = typeof<NUnit.Framework.TestFixtureAttribute>
let getattr el = Attribute.GetCustomAttributes(el, true)
let _ =
let asm = Assembly.LoadFile("some_asm_that_contains_nunit_tests.dll")
let ttypes = asm.GetTypes()
let allattrs = Seq.map getattr ttypes
printf "%A\n" allattrs
另一方面,在 REPL 中我更进一步。
> let z = ttypes.[0];;
val z : Type = Foo.Bar.Cow.Test.AaaTest
> Attribute.GetCustomAttributes(z, true);;
val it : Attribute [] =
[|NUnit.Framework.TestFixtureAttribute
{Arguments = [||];
Categories = null;
Category = null;
Description = null;
Ignore = false;
IgnoreReason = null;
TypeArgs = [||];
TypeId = NUnit.Framework.TestFixtureAttribute;}|]
本练习的目的是过滤掉没有 NUnit.Framework.TestFixtureAttribute 的方法(该代码未显示)。 ttypes 中的数组元素是异构的,例如上面显示的索引 0 类型为 Foo.Bar.Cow.Test.AaaTest 但索引 1 的类型为 Foo.Bar.Cow.Test.BbbTest 等。
像这样启动 fsharpi
fsharpi /r:/usr/lib/cli/nunit.framework-2.6/nunit.framework.dll
【问题讨论】:
-
实际的错误信息比错误代码更有用。
-
好的,添加了更多细节
-
ttypes 不是异构的,它是一个类型数组(例如,Type 类型的元素)。
-
另外,您要查找测试还是测试夹具?你似乎在你的问题中混淆了他们......
-
TestFixtures,刚刚修复