为什么F#不允许C#的多个属性?

Joh*_*han 8 f# attributes allowmultiple attributeusage

以下代码在C#中编译:

[ContentType("text")]
[ContentType("projection")]
public class Class1
{
}
Run Code Online (Sandbox Code Playgroud)

F#中的以下代码无法编译:

[<ContentType("text")>]
[<ContentType("projection")>]
type Class1() = class end
Run Code Online (Sandbox Code Playgroud)

F#中的编译错误是:"属性类型'ContentTypeAttribute'具有'AllowMultiple = false'.此属性的多个实例不能附加到单个语言元素."

通过反编译ContentType,我可以看到ContentType继承自MultipleUsase中具有'AllowMultiple = true'的 MultipleBaseMetadataAttribute .

实际上,似乎F#不从父类继承AttributeUsage.

[<AttributeUsage(AttributeTargets.Class, AllowMultiple = true)>]
type FooAttribute() = 
    inherit Attribute()

type BarAttribute() =
    inherit FooAttribute()

[<Foo>]
[<Foo>]
type MyClassCompiles() = class end
Run Code Online (Sandbox Code Playgroud)

哪里

[<Bar>]
[<Bar>]
type MyClassDoesNotCompile() = class end
Run Code Online (Sandbox Code Playgroud)

Dan*_*iel 8

看起来像个bug.电子邮件fsbugs [at] microsoft.com.这是另一个明显的错误:它似乎没有荣誉AttributeTargets:

[<AttributeUsage(AttributeTargets.Enum)>]
type FooAttribute() = 
  inherit Attribute()

[<Foo>]
type T = struct end //happily compiles
Run Code Online (Sandbox Code Playgroud)