【发布时间】: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