以下代码在 C# 中编译:
[ContentType("text")]
[ContentType("projection")]
public class Class1
{
}
F# 中的以下代码无法编译:
[<ContentType("text")>]
[<ContentType("projection")>]
type Class1() = class end
F# 中的编译错误是:“属性类型 'ContentTypeAttribute' 具有 'AllowMultiple=false'。此属性的多个实例不能附加到单个语言元素。”
通过反编译 ContentType,我可以看到 ContentType 继承自 MultipleBaseMetadataAttribute,它在 AttributeUsage 中具有“AllowMultiple=true”。
事实上,F# 似乎没有从父类继承 AttributeUsage。
[<AttributeUsage(AttributeTargets.Class, AllowMultiple = true)>]
type FooAttribute() =
inherit Attribute()
type BarAttribute() =
inherit FooAttribute()
[<Foo>]
[<Foo>]
type MyClassCompiles() = class end
在哪里
[<Bar>]
[<Bar>]
type MyClassDoesNotCompile() = class end