F# 尝试从程序集检索nunit测试属性时出现错误FS0001或FS0041

F# 尝试从程序集检索nunit测试属性时出现错误FS0001或FS0041,f#,nunit,F#,Nunit,以下F#脚本因FS0041而失败(无法根据此程序点之前的类型信息确定方法“GetCustomAttributes”的唯一重载。可能需要类型注释)。你怎么修理它 如果我添加类型注释,例如 let getattr (el : Sometype) = Attribute.CustomAttributes(el, true) 它与FS0001失败(类型“Sometype”与类型“Assembly”不兼容)。 可能过载:…省略长线…) 本练习的目的是筛选出没有NUnit.Framework.TestFi

以下F#脚本因FS0041而失败(无法根据此程序点之前的类型信息确定方法“GetCustomAttributes”的唯一重载。可能需要类型注释)。你怎么修理它

如果我添加类型注释,例如

let getattr (el : Sometype) = Attribute.CustomAttributes(el, true)
它与FS0001失败(类型“Sometype”与类型“Assembly”不兼容)。 可能过载:…省略长线…)

本练习的目的是筛选出没有NUnit.Framework.TestFixtureAttribute的方法(代码未显示)。 ttypes中的数组元素是异构的,例如上面显示的索引0 具有类型Foo.Bar.Cow.Test.AaaTest,但索引1具有类型Foo.Bar.Cow.Test.BbbTest等

开始吧,我喜欢这样

    fsharpi /r:/usr/lib/cli/nunit.framework-2.6/nunit.framework.dll

我认为你需要仔细阅读反思以及
类型实际上代表什么

要开始—如果要获取具有特定属性的类型,请检查以下脚本:

open System
open System.Reflection

/// an attribute and some types to test it on
type ExampleAttribute () = 
   inherit Attribute ()

[<Example>] type A = A
type B = B    
[<Example>] type C = C

/// get the types in an assembly
let types = Assembly.GetExecutingAssembly().GetTypes()

/// filter the types that are marked with ExampleAttribute (gives you A and C)
types 
|> Array.filter (fun typ -> typ.IsDefined(typeof<ExampleAttribute>))
开放系统
开放系统。反射
///一个属性和一些测试它的类型
类型ExampleAttribute()=
继承属性()
[]A型=A型
B型=B型
[]类型C=C
///获取程序集中的类型
让types=Assembly.getExecutionGassembly().GetTypes()
///过滤用ExampleAttribute标记的类型(提供A和C)
类型
|>Array.filter(乐趣类型->类型已定义(typeof))

请注意
typeof
运算符-这是从静态类型名称(代码中的ExampleAttribute字符串)到类型为
type
的对象的一种方式,它是ExampleAttribute类型的运行时表示形式。

实际错误消息比错误代码更有用。好,添加了一点更详细的信息类型不是异构的,它是一个类型数组(例如,类型类型的元素)。另外,是否要查找测试或测试装置?在你的问题中,你似乎把他们搞糊涂了……测试装置,刚刚修好,汉克斯,成功了。你能解释一下为什么管道语法消除了错误FS0072,换句话说,“Array.filter(fun-typ->typ.IsDefined(typeof))类型”没有work@snik197:这是类型推断在F#中如何工作的结果。通过管道,它在到达过滤器之前已经知道
类型的类型。这是一个有用的技巧,但它为您节省的只是编写注释-
Array.filter(fun(typ:Type)->…)类型
也同样有效。
    fsharpi /r:/usr/lib/cli/nunit.framework-2.6/nunit.framework.dll
open System
open System.Reflection

/// an attribute and some types to test it on
type ExampleAttribute () = 
   inherit Attribute ()

[<Example>] type A = A
type B = B    
[<Example>] type C = C

/// get the types in an assembly
let types = Assembly.GetExecutingAssembly().GetTypes()

/// filter the types that are marked with ExampleAttribute (gives you A and C)
types 
|> Array.filter (fun typ -> typ.IsDefined(typeof<ExampleAttribute>))