8

以下代码在 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
4

1 回答 1

8

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

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

[<Foo>]
type T = struct end //happily compiles
于 2012-09-27T14:30:12.340 回答