以下代码在C#中编译:
[ContentType("text")] [ContentType("projection")] public class Class1 { }F#中的以下代码不能编译:
[<ContentType("text")>] [<ContentType("projection")>] type Class1() = class endF#中的编译错误是: “属性类型'ContentTypeAttribute'具有'AllowMultiple = false'。该属性的多个实例不能附加到单个语言元素。”
通过反编译ContentType,我可以看到ContentType继承自AttributeUsage中'AllowMultiple = true'的 MultipleBaseMetadataAttribute。
事实上,似乎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 endThe following code compiles in C#:
[ContentType("text")] [ContentType("projection")] public class Class1 { }The following code in F# does not compile:
[<ContentType("text")>] [<ContentType("projection")>] type Class1() = class endThe compile error in F# is: "The attribute type 'ContentTypeAttribute' has 'AllowMultiple=false'. Multiple instances of this attribute cannot be attached to a single language element."
By decompiling ContentType, I can see that ContentType inherits from MultipleBaseMetadataAttribute which has 'AllowMultiple=true' in the AttributeUsage.
In fact, it seems like F# does not inherit the AttributeUsage from the parent class.
[<AttributeUsage(AttributeTargets.Class, AllowMultiple = true)>] type FooAttribute() = inherit Attribute() type BarAttribute() = inherit FooAttribute() [<Foo>] [<Foo>] type MyClassCompiles() = class endwhere
[<Bar>] [<Bar>] type MyClassDoesNotCompile() = class end最满意答案
看起来像一个错误。 电子邮件fsbugs [at] microsoft.com。 这是另一个显而易见的错误:它看起来并不尊重AttributeTargets :
[<AttributeUsage(AttributeTargets.Enum)>] type FooAttribute() = inherit Attribute() [<Foo>] type T = struct end //happily compilesLooks like a bug. Email fsbugs[at]microsoft.com. Here's another apparent bug: it doesn't appear to honor AttributeTargets:
[<AttributeUsage(AttributeTargets.Enum)>] type FooAttribute() = inherit Attribute() [<Foo>] type T = struct end //happily compiles
更多推荐
发布评论