编辑:添加了一个更完整的例子,以澄清问题。
一些.NET属性需要一个Type
类型的参数。在F#中如何声明这些参数?
例如,在C#中我们可以这样做:
[XmlInclude(typeof(Car))]
[XmlInclude(typeof(Truck))]
class Vehicle { }
class Car : Vehicle { }
class Truck : Vehicle { }
但是,在F#中以下代码:
[<XmlInclude(typeof<Car>)>]
[<XmlInclude(typeof<Truck>)>]
type Vehicle() = class end
type Car() = inherit Vehicle()
type Truck() = inherit Car()
...导致编译错误:这不是常量表达式或有效的自定义属性值。
type ... and ...
来定义递归类型(它们相互引用)。 - Tomas Petricek