【问题标题】:Why does F# not allow multiple attributes where C# does?为什么 F# 不允许 C# 允许的多个属性?
【发布时间】:2012-09-27 12:23:07
【问题描述】:

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

【问题讨论】:

  • 顺便说一句,这听起来与以下问题完全相同,但实际上并非如此。这是关于 F# 尊重属性上的 AllowMultiple 设置,另一个问题是关于在单个语句中组合多个属性的语法。 stackoverflow.com/questions/9620712/…

标签: f# attributes attributeusage allowmultiple


【解决方案1】:

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

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

[<Foo>]
type T = struct end //happily compiles

【讨论】:

  • 谢谢,我在 FSharp 团队中记录了这个错误。
猜你喜欢
  • 2017-01-11
  • 2012-01-27
  • 1970-01-01
  • 2014-05-23
  • 2011-04-26
  • 2018-06-20
  • 1970-01-01
  • 2017-02-21
  • 1970-01-01
相关资源
最近更新 更多